创建对象的动态LINQ [英] Creating a dynamic LINQ to Objects

查看:71
本文介绍了创建对象的动态LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的东西,我需要选定的过滤器变量在LINQ语句中的运行时中运行,而且我知道LING并没有按照我的预期进行.任何答案将不胜感激.

I have something like this where I need the selected filter variables to run at the runtime inside the LINQ statement, and I know the LING is not doing as I anticipate. Any answer would be appreciated.

//code starts here.......
string strOnGameWhere = " && t.GameType.Contains(Texas)";

var tournament = from t in tournamentName
                             where t.BeachBar == "Bar" + strOnGameWhere
                             select t;

推荐答案

Linq. 单击此处 [ Google it [
You can do this and many more by using expression trees in Linq. Click Here[^] for detail or Just google Google it[^].


我之前也遇到过同样的问题.解决方案1是正确的.
该论坛帖子对我实现目标有很大帮助:
http://social.msdn.microsoft.com/forums/zh-CN/linqprojectgeneral/thread/5edb3dd0-c778-47e2-b89d-a9c90a0bd1bc/
I had the same problem earlier. Solution 1 is correct.
This forum post helped me a lot to achieve my goal:
http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/5edb3dd0-c778-47e2-b89d-a9c90a0bd1bc/


一件显而易见的事情消失了"我看着您的代码是``strOnGameWhere''是字符串:当您将其添加到字符串"Bar"时,您当然会得到另一个字符串. />
显然,您打算要做的是让LINQ评估正在进行中,作为"Where"子句的一部分评估条件
One obvious thing that ''leaps out'' at me looking at your code is that ''strOnGameWhere'' is a string: when you add it to the string ''Bar,'' of course you just get another string.

Clearly what you intend to do is to have the LINQ evaluation in progress evaluate the condition
&& t.GameType.Contains(Texas)

,因此执行的是:

where t.BeachBar == "Bar" && t.GameType.Contains(Texas)

So,to对我来说,您要解决的问题是如何在执行Where子句的LINQ查询中调用变量函数.

用LINQ术语来说,据我所知,这意味着LINQ表达式必须捕获"一个外部"变量引用,在这种情况下是一个函数? ... lambda吗?

为了理解这一点,以及.NET中的其他内容,我建议您阅读并阅读Jon Skeet的杰作《深度C#》(曼宁出版社,第二版).

So, to me, the issue you are dealing with is how to call a variable function in a LINQ query that is executed inside a Where clause.

In LINQ terms, as I understand them, that means the LINQ expression must ''capture'' an ''outer'' variable reference, in this case ... a function ? ... a lambda ?

For understanding that, and so much else in .NET, I suggest you get and read Jon Skeet''s masterpiece, "C# in Depth" (Manning Press, 2nd. edition).


这篇关于创建对象的动态LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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