Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
如何在动态linq表达式中使用带有Func参数的linq扩展?_Linq_Extension Methods_Dynamic Linq - Fatal编程技术网

如何在动态linq表达式中使用带有Func参数的linq扩展?

如何在动态linq表达式中使用带有Func参数的linq扩展?,linq,extension-methods,dynamic-linq,Linq,Extension Methods,Dynamic Linq,尽管有些强大,System.Linq.Dynamic库却惊人地缺少文档,特别是在更复杂的查询必须遵循哪些约定方面 在我正在处理的查询中,它包含一个FirstOrDefault调用,但我似乎无法让它工作 以下是整个(非工作)表达式: "Locations.FirstOrDefault(x => x.IsPrimaryLocation).Address1 as Address" 我可以编写这个FirstOrDefault表达式来处理动态linq吗 编写此表达式的正确方法是什么?扩展动态库当然

尽管有些强大,System.Linq.Dynamic库却惊人地缺少文档,特别是在更复杂的查询必须遵循哪些约定方面

在我正在处理的查询中,它包含一个
FirstOrDefault
调用,但我似乎无法让它工作

以下是整个(非工作)表达式:

"Locations.FirstOrDefault(x => x.IsPrimaryLocation).Address1 as Address"
我可以编写这个FirstOrDefault表达式来处理动态linq吗


编写此表达式的正确方法是什么?

扩展动态库当然是一个选项,正如前面所建议的那样。 动态Linq中给定
,其中
的另一个选项返回Iqueryable

 public static class DynamicQueryable {

    public static IQueryable<T> Where<T>(this IQueryable<T> source, string predicate, params object[] values) { return (IQueryable<T>) Where((IQueryable) source, predicate, values); }

    public static IQueryable Where(this IQueryable source, string predicate, params object[] values)       {
如果需要的话,也可以是动态的

DynamicLocations.Where("IsPrimaryLocation",new string[]).FirstOrDefault( ).Address1 as Address;
详细信息: 可以在实例化为动态存储库的泛型存储库类上的方法上公开

     public virtual IQueryable<TPoco> DynamicWhere(string predicate, params object[] values) {
        return AllQ().Where(predicate, values);
    }
公共虚拟IQueryable DynamicWhere(字符串谓词,参数对象[]值){
返回AllQ().Where(谓词,值);
}
动态通用存储库实例化示例

  public class RepositoryFactory<TPoco>  where TPoco : BaseObject,new() {

    public IRepositoryBase<TPoco> GetRepository(DbContext context) {

       // get the Pocotype for generic repository instantiation
        var pocoTypes = new[] {typeof (TPoco)};  // but supports <T,U>
        Type repBaseType = typeof (RepositoryBase<>);

        IRepositoryBase<TPoco> repository = InstantiateRepository(context, repBaseType, pocoTypes);

        return repository;
    }


    private IRepositoryBase<TPoco> InstantiateRepository(DbContext context, Type repType, params Type[] args) {

        Type repGenericType = repType.MakeGenericType(args);
        object repInstance = Activator.CreateInstance(repGenericType, context);
        return (IRepositoryBase<TPoco>)repInstance;
    }

}
公共类RepositoryFactory,其中TPoco:BaseObject,new(){ 公共IRepositoryBase GetRepository(DbContext上下文){ //获取通用存储库实例化的Pocotype var pocoTypes=new[]{typeof(TPoco)};//但支持 类型repBaseType=typeof(RepositoryBase); IRepositoryBase repository=实例化存储库(上下文、repBaseType、pocoTypes); 返回存储库; } 私有IRepositoryBase实例化存储(DbContext上下文,类型repType,参数类型[]args){ 类型repGenericType=repType.MakeGenericType(args); 对象repInstance=Activator.CreateInstance(repGenericType,上下文); 返回(IRepositoryBase)repInstance; } }
与动态linq无关,但是
FirstOrDefault()
可以返回一个
null
值,当您尝试访问
Address1
(或任何)属性时,该值将引发异常。确实,尽管我的特定问题在到达该点之前发生,因为它无法正确解析字符串。我会接受一个答案,用First()而不是FirstOrDefault()来解决这个问题。这可能会有帮助,所以我想,必须编辑Linq.Dynamic的源代码,并在中添加FirstOrDefault方法。看起来像这样。由于没有使用DL,我真的不能说。
  public class RepositoryFactory<TPoco>  where TPoco : BaseObject,new() {

    public IRepositoryBase<TPoco> GetRepository(DbContext context) {

       // get the Pocotype for generic repository instantiation
        var pocoTypes = new[] {typeof (TPoco)};  // but supports <T,U>
        Type repBaseType = typeof (RepositoryBase<>);

        IRepositoryBase<TPoco> repository = InstantiateRepository(context, repBaseType, pocoTypes);

        return repository;
    }


    private IRepositoryBase<TPoco> InstantiateRepository(DbContext context, Type repType, params Type[] args) {

        Type repGenericType = repType.MakeGenericType(args);
        object repInstance = Activator.CreateInstance(repGenericType, context);
        return (IRepositoryBase<TPoco>)repInstance;
    }

}