简体   繁体   English

使用linq用动态条件字符串过滤通用列表

[英]Filter a generic list with a dynamic condition string with linq

I am using .NET 3.5 我正在使用.NET 3.5

I have a List of Company.cs objects. 我有一个Company.cs对象的列表。

I want to filder a this list dynamically with LINQ that means the user enters this string in a textbox: 我想使用LINQ动态地填充此列表,这意味着用户在文本框中输入以下字符串:

(CompanyName = "Google") OR 
(Country = "Czech Republic") or
 (CompanyName = "Microsoft") AND
 (Country = "USA")

How can I use this string together with linq and a .Where() clause to filter the existing company objects? 如何将该字符串与linq和.Where()子句一起使用以筛选现有公司对象?

Or is it better at all to use a DataTable and use the Select method? 还是使用DataTable并使用Select方法根本更好?

Check out the Dynamic LINQ library . 查看动态LINQ库

Edit: 编辑:

Sorry, I've misunderstood you. 对不起,我误会了你。

If you have company and country lists like: 如果您有公司和国家/地区清单,例如:

var compNames = new[] { "Boo", "Foo" };
var countries = new[] { "Germany", "Bubu" };

Then you can filter like this: 然后,您可以像这样进行过滤:

from c in companies 
where compNames.Contains(c.Name) || countries.Contains(c.Country)
select c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM