带有数据对象的动态 LINQ
所以我一直在寻找一个简单的例子来解决一个简单的问题。
我有一个简单的对象,一个 ListTest (List) 列表,
public class ListTest{
{
public string perName { get; set; }
public string perSex { get; set; }
public ListTest(string pName, string pSex)
{
this.perSex = pSex;
this.perName = pName;
}
}
我已经加载了一些数据:
List<ListTest> tryIt = new List<ListTest>();
tryIt.Add(new ListTest("Karen", "F"));
tryIt.Add(new ListTest("Kate", "F"));
tryIt.Add(new ListTest("Glen", "M"));
tryIt.Add(new ListTest("Tiger", "M"));
tryIt.Add(new ListTest("Clementine", "F"));
tryIt.Add(new ListTest("Magnolia", "F"));
现在我想使用 Lambda 表达式来查询它:
var things = tryIt
.Where(sex => (sex.perSex == "F"))
.OrderBy(sex => sex.perName);
但我想动态地执行此操作,以防万一我想更改我的位置“名字”。
我能够创建您的表达式的 Lambda 表达式,但我无法弄清楚如何将其跨越目标线并实际将其分配给 where 子句并执行它。
IQueryable<ListTest> iq = tryIt.AsQueryable();
ParameterExpression pe = Expression.Parameter(typeof(ListTest), "Person");
Expression paEx = Expression.Property(pe, "perSex");
Expression right = Expression.Constant("F");
Expression eqEx = Expression.Equal(paEx, right);
Expression lE = Expression.Lambda<Func<ListTest, bool>>(eqEx, pe);
这应该是一个简单的 4 或 5 行解决方案,但我找不到易于理解的解决方案示例。
我应该使用 MethodCallExpression 还是类似的东西?
谢谢,
So I have been searching for a simple example to hopefully a simple problem.
I have a simple object a List of ListTest (List)
public class ListTest{
{
public string perName { get; set; }
public string perSex { get; set; }
public ListTest(string pName, string pSex)
{
this.perSex = pSex;
this.perName = pName;
}
}
I have loaded it with some data:
List<ListTest> tryIt = new List<ListTest>();
tryIt.Add(new ListTest("Karen", "F"));
tryIt.Add(new ListTest("Kate", "F"));
tryIt.Add(new ListTest("Glen", "M"));
tryIt.Add(new ListTest("Tiger", "M"));
tryIt.Add(new ListTest("Clementine", "F"));
tryIt.Add(new ListTest("Magnolia", "F"));
Now I want to query it using a Lambda Expression:
var things = tryIt
.Where(sex => (sex.perSex == "F"))
.OrderBy(sex => sex.perName);
But I want to do it dynamically, just in case I want to change my where to "perName".
I am able to create the Lambda Expression your Expressions, but I can't figure out how to take it across the goal line and actually assign it to a where clause and execute it.
IQueryable<ListTest> iq = tryIt.AsQueryable();
ParameterExpression pe = Expression.Parameter(typeof(ListTest), "Person");
Expression paEx = Expression.Property(pe, "perSex");
Expression right = Expression.Constant("F");
Expression eqEx = Expression.Equal(paEx, right);
Expression lE = Expression.Lambda<Func<ListTest, bool>>(eqEx, pe);
This should be a simple 4 or 5 line solution, but I can't find an easily decipherable solution example.
Should I use a MethodCallExpression or something down those lines?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试这样的操作:
编辑:
或者,还有流行的LINQ 动态查询库,您可以在其中几乎按照您想要的方式形成查询(当然是动态的) )。
You can try something like this:
Edit:
Or, there's also the popular LINQ Dynamic Query Library, where you can form your query pretty much however you want (dynamically, of course).