动态构建 lambda 表达式
因此,我开始构建一个小型测试应用程序来测试 lambda 表达式。 我在这里和其他地方找到了几个例子,但我就是不明白。
谁能解释我如何使用文本框或任何其他变量构建表达式?
我的测试列表
List<People> lPeople = new List<People>
{
new People { Name= "Jean", LastName = "Borrow", Age= 21 } ,
new People { Name= "Dean", LastName = "Torrow", Age= 20 }
};
工作 lambda 表达式
IEnumerable<People> result = lPeople.Where(p => p.Age < 21);
dgv_1.DataSource = result.ToList();
dgv_1.Update();
如何动态构建表达式?
类似于 lPeople.Where(p => p.LastName == Textbox.Text);
(这当然不起作用)
谢谢!
编辑:在下面的解决方案中添加了一些代码
Int32 iAge;
Boolean bSuc = Int32.TryParse(tb_filter_age.Text, out iAge);
if (!bSuc)
{
iAge = 0;
}
So, I started to build a small test application to test lambda expressions. I found several examples here and elsewhere but I just don't get them.
Can anybody explain my how to build an expression by using textboxes or any other variables?
My Test List
List<People> lPeople = new List<People>
{
new People { Name= "Jean", LastName = "Borrow", Age= 21 } ,
new People { Name= "Dean", LastName = "Torrow", Age= 20 }
};
Working lambda Expression
IEnumerable<People> result = lPeople.Where(p => p.Age < 21);
dgv_1.DataSource = result.ToList();
dgv_1.Update();
How can I build the expressions dynamically?
Something like lPeople.Where(p => p.LastName == Textbox.Text);
(which of course doesn't work)
Thanks!
Edit: Added some code to the solution below
Int32 iAge;
Boolean bSuc = Int32.TryParse(tb_filter_age.Text, out iAge);
if (!bSuc)
{
iAge = 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
“这当然行不通”
当你尝试时会发生什么? 从表面上看,这就是我一直在做的事情。
基于指定运算符的 ComboBox 切换操作:
如果用户输入无法转换的内容,则将年龄字符串转换为
int
的代码将抛出异常。 查找TryParse
以避免异常。"which of course doesn't work"
What happens when you try it? By the look of it, that's the kind of thing I do all the time.
To switch operations based on a ComboBox specifying the operator:
The code that converts the age string into an
int
will throw if the user enters something that can't be converted. Look upTryParse
to avoid exceptions.尝试使用谓词生成器 http://www.albahari.com/nutshell/predicatebuilder.aspx
我用它进行高级搜索,用户可以不断添加可选的搜索条件。
Try the Predicate Builder at http://www.albahari.com/nutshell/predicatebuilder.aspx
I used it to make an advanced search where the user could keep adding optional search criteria.
您可以使用 Linq 动态查询库来完成此任务。 有关详细信息,请参阅 Scott Guthrie 的以下博客文章:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-动态查询库.aspx
You can use the Linq Dynamic Query Library to accomplish this. See the following blog post from Scott Guthrie for more information:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
您的示例 lambda 表达式将起作用。 您需要它有多动态? 如果您有要应用于集合的“过滤器”静态 UI,则可以创建类似于以下内容的代码:
如果您希望数据源的使用者应用真正的动态表达式(使他们能够选择其他字段进行过滤)以及要使用的表达式),这是使用谓词构建器工具或 LINQ 表达式对象实现的更复杂的功能。
Your example lambda expression will work. How dynamic do you need it to be? If you have a static UI of 'filters' to apply to a collection, you can create code similar to the following:
If you want the consumer of your data source to apply truly dynamic expressions (afford them the ability to choose other fields to filter and the expressions to use), that's a more complicated feature to implement using a predicate builder tool or LINQ Expression objects.
你处理这件事的方式应该没有什么问题。 我创建了一个简单的 Windows 窗体应用程序,其中包含一个
TextBox
、一个Button
和一个DataGridView
(名称为textBox1
、button1
和dgv_1
。)以下是我用于按预期工作的
Form1.cs
文件的代码:There should be nothing wrong with the way you're going about it. I have created a simple Windows Forms Application with a
TextBox
, aButton
, and aDataGridView
(with namestextBox1
,button1
, anddgv_1
respectively.)Here is the code I used for the
Form1.cs
file that worked as expected:在动态用户选择的情况下,我认为比使用 if 块更优雅的解决方案是声明一个变量,
根据用户选择设置其值
,然后将其传递给Where 子句:
In case of dynamic user choice, I think that a more elegant solution rather then using if blocks would be to declare a variable
set its value based upon the user choice
and then pass it to the Where clause: