如何在字符串列表中使用where
how to use where with list of string
我有像 "one" , "two" , "three",
这样的字符串列表
我想从数据库中搜索 select 至少包含其中一项的所有项目,我想通过 entity framework 执行此操作。为此,我使用:
if (SearchVM.Tags.Count > 0)
//SourceList= SourceList.Where(x=>x.Tags.Where(...));
它不起作用,因为它是错误的和不完整的。
通过 T-sql 是:
select * from SourceList where tagname in (select name from SearchList)
试试这个:
var SearchList= new List<string>{"one" , "two" , "three" };
var result = dataContext.SourceList.Where(p => p.Tags.Any(t => SearchList.Contains(t.TagName)));
Contains
将被翻译成 IN
语句
我有像 "one" , "two" , "three",
这样的字符串列表我想从数据库中搜索 select 至少包含其中一项的所有项目,我想通过 entity framework 执行此操作。为此,我使用:
if (SearchVM.Tags.Count > 0)
//SourceList= SourceList.Where(x=>x.Tags.Where(...));
它不起作用,因为它是错误的和不完整的。
通过 T-sql 是:
select * from SourceList where tagname in (select name from SearchList)
试试这个:
var SearchList= new List<string>{"one" , "two" , "three" };
var result = dataContext.SourceList.Where(p => p.Tags.Any(t => SearchList.Contains(t.TagName)));
Contains
将被翻译成 IN
语句