动态选择EntityFramework和System.Linq.Dynamic [英] Dynamic Select EntityFramework and System.Linq.Dynamic

查看:124
本文介绍了动态选择EntityFramework和System.Linq.Dynamic的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用DynamicQueryable.Select()//动态选择库



到目前为止我发现的唯一文件是 / p>

我有这个匿名返回类型的查询工作。

  var result = Context.Set< TPoc​​oText>()。Where((< Func< TPoc​​oText,bool> whereLambda)
.OrderByDescending(t => t.RowVersion).Skip(skip).Take(take)
.Select(new(m1,m2,Nav1));

,这样就像 select(t => new {t.m1 ,t.m2,t.Nav1})如预期



我的问题
如何做到相当于选择(t => new {t,t.Nav1})



我试过。选择(new(it,Nav1))
和.Select(new(this,Nav1))



结果是未找到解析错误成员。
任何人都知道这个动态字符串解析API?



或者等效的表达式构建语法也是一个选项。 b
$ b

注意:Nav Property ForSourceRecord只在运行时才知道,否则我只会使用正常的lambda表达式。

解决方案

这不能使用anon类型。编译器没有工作所需的信息,特别是使用免费的文本/字符串方法。



尝试这个...

  var param = System.Linq .Expressions.Expression.Parameter(typeof运算(TPocoText)); 
var init = System.Linq.Expressions.Expression.MemberInit(
System.Linq.Expressions.Expression.New(typeof(Foo)),
new [] {
系统.Linq.Expressions.Expression.Bind(GetMemberInfo((Foo f)=> f.Nav),System.Linq.Expressions.Expression.PropertyOrField(param,NameOfPropertyToBindToNav)),
System.Linq.Expressions .Expression.Bind(GetMemberInfo((Foo f)=> f.M1),System.Linq.Expressions.Expression.PropertyOrField(param,M1)),
}
);
var result = Context.Set< TPoc​​oText>()。其中​​((< Func< TPoc​​oText,bool>>)whereLambda)
.OrderByDescending(t => t.RowVersion).Skip skip(take)
.Select(System.Linq.Expressions.Expression.Lambda&FunC< TPoc​​oText,Foo>>(init,param));



public class Foo
{
public string Nav {get; set;}
public string M1 {get; set;}
}
public static MemberInfo GetMemberInfo< T,U>(表达式< Func< T,U>>表达式)
{
var member = expression.Body as MemberExpression;
if(member!= null)
return member.Member;

抛出新的ArgumentException(表达式不是成员访问,表达式);
}


Im using the DynamicQueryable.Select() // dynamic select library

The only docu I have found so far is System.Linq.Dynamic docu

I have this query with anonymous return type which works.

var result = Context.Set<TPocoText>().Where((Expression<Func<TPocoText, bool>>) whereLambda)
                     .OrderByDescending(t => t.RowVersion).Skip(skip).Take(take)
                     .Select("new (m1,m2,Nav1) ");

and this works like select(t=> new {t.m1,t.m2,t.Nav1}) as expected

my Question How can it do the equivalent of select(t=> new {t,t.Nav1})

i tried .Select("new (it,Nav1) ") and .Select("new (this,Nav1) ")

the result was a parse error member not found. Anybody know this dynamic string parsing API?

OR the equivalent Expression building syntax is also an option.

NOTE:The Nav Property ForSourceRecord is only known at runtime otherwise i would just use the normal lambda expression.

解决方案

This cannot be done using anon types. The compiler just doesn't have the information needed to work, especially with the free text/string approach.

Try this...

var param = System.Linq.Expressions.Expression.Parameter(typeof(TPocoText));
var init = System.Linq.Expressions.Expression.MemberInit(
    System.Linq.Expressions.Expression.New(typeof(Foo)),
    new []{
        System.Linq.Expressions.Expression.Bind(GetMemberInfo((Foo f) => f.Nav), System.Linq.Expressions.Expression.PropertyOrField(param, "NameOfPropertyToBindToNav")),
        System.Linq.Expressions.Expression.Bind(GetMemberInfo((Foo f) => f.M1), System.Linq.Expressions.Expression.PropertyOrField(param, "M1")),
    }
);
var result = Context.Set<TPocoText>().Where((Expression<Func<TPocoText, bool>>) whereLambda)
                 .OrderByDescending(t => t.RowVersion).Skip(skip).Take(take)
                 .Select(System.Linq.Expressions.Expression.Lambda<Func<TPocoText, Foo>>(init, param));



public class Foo
{
    public string Nav {get;set;}
    public string M1 {get;set;}
}
public static MemberInfo GetMemberInfo<T, U>(Expression<Func<T, U>> expression)
{
    var member = expression.Body as MemberExpression;
    if (member != null)
        return member.Member;

    throw new ArgumentException("Expression is not a member access", "expression");
}

这篇关于动态选择EntityFramework和System.Linq.Dynamic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆