[英]How to use Linq to check if a list of strings contains any string in a list
I'm constructing a linq query that will check is a string in the DB contains any of the strings in a list of strings.我正在构建一个 linq 查询,它将检查数据库中的字符串是否包含字符串列表中的任何字符串。
Something like.就像是。
query = query.Where(x => x.tags
.Contains(--any of the items in my list of strings--));
I'd also like to know how many of the items in the list were matched.我还想知道列表中有多少项目是匹配的。
Any help would be appreciated.任何帮助,将不胜感激。
Update: I should have mentioned that tags is a string not a list.更新:我应该提到标签是一个字符串而不是一个列表。 And I am adding on a couple more wheres that are not related to tags before the query actually runs.
我在查询实际运行之前添加了更多与标签无关的位置。 This is running against entity framework.
这是针对实体框架运行的。
EDIT: This answer assumed that tags
was a collection of strings...编辑:这个答案假设
tags
是一个字符串的集合......
It sounds like you might want:听起来你可能想要:
var list = new List<string> { ... };
var query = query.Where(x => x.tags.Any(tag => list.Contains(tag));
Or:或者:
var list = new List<string> { ... };
var query = query.Where(x => x.tags.Intersect(list).Any());
(If this is using LINQ to SQL or EF, you may find one works but the other doesn't. In just LINQ to Objects, both should work.) (如果这是使用 LINQ to SQL 或 EF,您可能会发现其中一个有效,但另一个无效。仅在 LINQ to Objects 中,两者都应该有效。)
To get the count, you'd need something like:要获得计数,您需要以下内容:
var result = query.Select(x => new { x, count = x.tags.Count(tag => list.Contains(tag)) })
.Where(pair => pair.count != 0);
Then each element of result
is a pair of x
(the item) and count
(the number of matching tags).然后
result
每个元素是一对x
(项目)和count
(匹配标签的数量)。
I've done something like this before:我以前做过这样的事情:
var myList = new List<string>();
myList.Add("One");
myList.Add("Two");
var matches = query.Where(x => myList.Any(y => x.tags.Contains(y)));
I am not quite sure from your question if x.tags is a string or list, if it is a list Jon Skeet's answer is correct.我不太确定 x.tags 是字符串还是列表,如果它是列表,Jon Skeet 的回答是正确的。 If I understand you correctly though x.tags is a string of strings.
如果我正确理解你的话,虽然 x.tags 是一串字符串。 If so then the solution is:
如果是这样,那么解决方案是:
list.Any(x => x.tags.IndexOf(x) > -1)
to count them do数数他们做
list.Count(x => x.tags.IndexOf(x) > -1)
like this:像这样:
List<string> list = new List<string>();
list.Add("One");
list.Add("Two");
var result = query.Where(x => list.Contains(x.tags));
var t = new List<string> { "a", "b", "c" };
var y = "abd"; var y = "abd";
var res = y.Count(x => t.Contains(x.ToString())); var res = y.Count(x => t.Contains(x.ToString()));
I faced a similar problem recently and here's how I managed to work it out:我最近遇到了类似的问题,这是我设法解决的方法:
var list = [list of strings];
if (list != null && list.Any())
{
queryable = queryable.Where(x => x.tags != null);
var tagQueries = new List<IQueryable<WhateverTheDbModelIs>>();
foreach (var element in list)
{
tagQueries.Add(queryable.Where(x => x.tags.Contains(element)));
}
IQueryable<WhateverTheDbModelIs> query = tagQueries.FirstOrDefault();
foreach (var tagQuery in tagQueries)
{
query = query.Union(tagQuery);
}
queryable = queryable.Intersect(query);
}
probably not the best option but something a less experienced developer can understand and use可能不是最好的选择,但经验不足的开发人员可以理解和使用的东西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.