Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重用在可移植类库中创建的LINQ表达式_C#_.net_Windows Phone 7_Compiled Query_Portable Class Library - Fatal编程技术网

C# 重用在可移植类库中创建的LINQ表达式

C# 重用在可移植类库中创建的LINQ表达式,c#,.net,windows-phone-7,compiled-query,portable-class-library,C#,.net,Windows Phone 7,Compiled Query,Portable Class Library,是否可以在可移植类库中创建LINQ表达式,并在应用程序的其他部分重用该表达式?对我来说,一个方便的用例是在跨平台的CompiledQuerys中使用这些表达式,而不是多次复制/粘贴相同的代码 到目前为止,我所有的尝试都或多或少地引发了一些毫无意义的运行时异常(NullReferenceException,Column not found..)。我猜PCL中的LINQ表达式的处理方式与WP应用程序中的不同 public class SomeClassNotInPCL { private s

是否可以在可移植类库中创建LINQ表达式,并在应用程序的其他部分重用该表达式?对我来说,一个方便的用例是在跨平台的CompiledQuerys中使用这些表达式,而不是多次复制/粘贴相同的代码

到目前为止,我所有的尝试都或多或少地引发了一些毫无意义的运行时异常(
NullReferenceException
,Column not found..)。我猜PCL中的LINQ表达式的处理方式与WP应用程序中的不同

public class SomeClassNotInPCL
{
    private static readonly Func<Context, int, MyClass> CompiledQuery = 
        CompiledQuery.Compile(ClassFarAway.MethodInsidePCL());
}

public class ClassFarAway
{
    private static Expression<Func<IContext, int, MyClass>> MethodInsidePCL()
    {
        return (context, id) => context.MyClass.FirstOrDefault(m => m.Id == id);
    }
}
公共类SomeClassNotInPCL
{
私有静态只读Func CompiledQuery=
CompiledQuery.Compile(ClassFarAway.MethodInsidePCL());
}
公务舱
{
私有静态表达式MethodInsidePCL()
{
return(context,id)=>context.MyClass.FirstOrDefault(m=>m.id==id);
}
}

像这样的东西怎么样

var commonExpressions = new Dictionary<CommonRegEx, Regex>();

public enum CommonRegEx{
   Phone,
   Email,
   SSN
}
var commonExpressions=new Dictionary();
公共枚举CommonRegEx{
电话,
电子邮件,
SSN
}

Linq表达式或正则表达式中的表达式?很好。我对System.Linq.Expressions.Expression.Sample代码非常感兴趣。这是L2SQL吗?L2EF?L2O?谢谢你的反馈。我认为您的示例完全没有问题,它可能会很有魅力,但是,我希望在多个平台之间重用Linq表达式。