4

我有一个需要在表中匹配的部分字符串列表。我正在使用 PredicateBuilder。

var predicate = PredicateBuilder.False<Name>();
List<string> names = new List<string>();
names.Add("test name"); **<===matches**
names.Add("test"); **<=== doesn't match**
predicate = predicate.Or(n => names.Contains(n.Company));
var results = (from n in Names
.AsExpandable()
.Where(predicate)
select(new{ n.Company}));

n.Company = "测试名称"

如果 n.Company 恰好是“测试名称”,这将匹配,但如果我只使用“测试”,则它不匹配。如何匹配 list.Contains 上的部分内容?

4

1 回答 1

6

您应该以这种方式更改代码

var predicate = PredicateBuilder.False<Name>();
List<string> names = new List<string>();
names.Add("test name"); 
names.Add("test"); 
foreach(string name in names)    
{
    string temp = name;
    predicate = predicate.Or(n => n.Company.Contains(temp));
}
var results = (from n in Names 
    .AsExpandable()
    .Where(predicate)
    select(new{ n.Company}));
于 2013-01-16T01:10:48.310 回答