linq查询不包含某个值的记录,使用LINQ查询包含不包含

I am trying to fetch records from database in the sense that

it should getrecords where name contains "searchKey" and name not in the excludeTerms array with comma seperated.

How can I do this in 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();

where excludeterma contains list of elements suppose

1)Sandy

2)Mandy

3)Larry etc

List excludeTerms = new List();

解决方案

Unfortunately you can use local sequences only with Contains operator (which is translated into SQL IN operator). So, you can move whole filtering to memory

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();

Or just filtering out excluded terms:

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值