动态选择EntityFramework和System.Linq.Dynamic [英] Dynamic Select EntityFramework and System.Linq.Dynamic
问题描述
我使用DynamicQueryable.Select()//动态选择库
到目前为止我发现的唯一文件是 / p> 我有这个匿名返回类型的查询工作。 ,这样就像 我的问题 我试过。选择(new(it,Nav1)) 结果是未找到解析错误成员。 或者等效的表达式构建语法也是一个选项。 b 注意:Nav Property ForSourceRecord只在运行时才知道,否则我只会使用正常的lambda表达式。 这不能使用anon类型。编译器没有工作所需的信息,特别是使用免费的文本/字符串方法。 尝试这个... 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. and this works like 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...
这篇关于动态选择EntityFramework和System.Linq.Dynamic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
var result = Context.Set< TPocoText>()。Where((< Func< TPocoText,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})
和.Select(new(this,Nav1))
任何人都知道这个动态字符串解析API?
$ b
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< TPocoText>()。其中((< Func< TPocoText,bool>>)whereLambda)
.OrderByDescending(t => t.RowVersion).Skip skip(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>(表达式< Func< T,U>>表达式)
{
var member = expression.Body as MemberExpression;
if(member!= null)
return member.Member;
抛出新的ArgumentException(表达式不是成员访问,表达式);
}
var result = Context.Set<TPocoText>().Where((Expression<Func<TPocoText, bool>>) whereLambda)
.OrderByDescending(t => t.RowVersion).Skip(skip).Take(take)
.Select("new (m1,m2,Nav1) ");
select(t=> new {t.m1,t.m2,t.Nav1})
as expectedvar 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");
}