LINQ包含2个列表之间 [英] LINQ contains between 2 lists
问题描述
我有一个字符串列表和一个供应商列表<供应方式>
I have a string List and a supplier List<supplier>
.
字符串列表包含一些搜索项目和供应商列表中包含的供应商对象的列表。
现在我需要找到所有与任何项目的字符串相匹配的供应商名称列表<字符串方式>
string list contains some searched items and supplier list contains a list of supplier object.
Now I need to find all the supplier names that matches with any of the items in the string List<string>
.
这是我失败的尝试之一。
this is one of my failed attempts..
var query = some join with the supplier table.
query = query.where(k=>stringlist.contains(k.companyname)).select (...).tolist();
任何想法如何做到这一点?
any idea how to do that??
可能是我的问题不够清楚......我需要找到供应商(不只是名字,整个对象),其中供应商的名单在字符串列表中的任何项目相匹配。
May be my question is not clear enough...I need to find a list of suppliers(not only names,the whole object) where suppliers names matches with the any items in the string list.
如果我
query = query.where(k=>k.companyname.contains("any_string")).select (...).tolist();
它的工作原理。但是这不是我的要求。
我的要求是字符串列表不是一个字符串。
it works. but this is not my requirement. My requirement is a list of string not a single string.
推荐答案
下面的查询将返回不同的供应商的名称也存在在名单:
Following query will return distinct suppliers names which exist in list of names:
suppliers.Where(s => stringlist.Contains(s.CompanyName))
.Select(s => s.CompanyName) // remove if you need whole supplier object
.Distinct();
生成的SQL查询将如下所示:
Generated SQL query will look like:
SELECT DISTINCT [t0].[FCompanyName]
FROM [dbo].[Supplier] AS [t0]
WHERE [t0].[CompanyName] IN (@p0, @p1, @p2)
BTW考虑用更好的名称,例如: companyNames
而不是的StringList
BTW consider to use better names, e.g. companyNames
instead of stringlist
这篇关于LINQ包含2个列表之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!