3

我在这里找到了一个例子:

var foo = things.Where(data => myList.Contains(data.Title));

然而,这是精确的字符串匹配。如果 data.Title.ToLower() 包含在列表 ToLower() 中找到的任何字符串,我只对它感兴趣。

说列表有苹果,在里面预定时钟。

data.Title 例如:Apple Jacks、Book Club、Clockwork Book,这些都可以。但是 Claws and Foods、Clicks、Application Fundamentals 将不被接受。

4

2 回答 2

4

您要查找Title可能包含的每个项目(忽略大小写):

var foo = things.Where(data => myList
                .Any(item => data.Title.ToLower().Contains(item.ToLower())));
于 2013-04-05T13:29:32.723 回答
1
var myList = new List<string> { "apple", "book", "ClocK" };
var things = new List<string> { "Apple Jacks", "Book Club", "Clockwork Book", "Claws and Foods", "Clicks", "Application Fundamentals" };
var myRegex = new Regex(string.Join("|", myList.Select(x => Regex.Escape(x))), RegexOptions.IgnoreCase);
foreach (var matchingThing in things.Where(x => myRegex.IsMatch(x)))
    Console.WriteLine(matchingThing);

输出:

Apple Jacks
Book Club
Clockwork Book
于 2013-04-05T13:34:29.523 回答