Csharp/C#教程:Linq查询使用包含而不包含分享


Linq查询使用包含而不包含

我试图从数据库中获取记录,因为它应该获取记录,其中name包含“searchKey”,而名称不在excludeTerms数组中,并且逗号分隔。 我怎么能在Linq做到这一点?

Rows = (from u in DB.Clients where u.Name.Contains(searchTerm) && !u.Name.Contains(string.Join(",", excludeTerms.Select(s => "'" + s + "'").ToArray())) select new ClientModel { Name = u.FullName, Id = u.Id, }).Take(5).ToList(); 

其中excludeterma包含假设的元素列表

 1)Sandy 2)Mandy 3)Larry etc 

 List excludeTerms = new List(); 

不幸的是,您只能将本地序列与Contains运算符(将其转换为SQL IN运算符)一起使用。 因此,您可以将整个过滤移动到内存中

 Rows = (from u in DB.Clients.AsEnumerable() where u.Name.Contains(searchTerm) && !excludeTerms.Any(s => u.Name.Contains(s)) select new ClientModel { Name = u.FullName, Id = u.Id, }).Take(5).ToList(); 

或者只是过滤掉排除的术语:

上述就是C#学习教程:Linq查询使用包含而不包含分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 Rows = (from u in DB.Clients where u.Name.Contains(searchTerm) select new ClientModel { Name = u.FullName, Id = u.Id, }).AsEnumerable() .Where(m => !excludeTerms.Any(s => m.Name.Contains(s))) .Take(5).ToList(); 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/987975.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐