开发者

Linq - doesn't start with any prefix in range of prefixes

I need to create a Linq query that would have the following logic:

IEnumerable<string> prefixes = GetListOfPrefixesFromSomewhere();
IQueryable<Record> myQuery = GetAllRecordsFromRepository();
foreach (string prefix in prefixes)
{
  myQuery = myQuery.Where(x => !x.Field.StartsWith(prefix));
}

This would obviously result in a large IQueryable which can then be exe开发者_如何学运维cuted.

Is there a nice elegant way to express this is a single Linq statement?


You can at least try this:

// Only call ToList if you need to, of course... but I think EF/LINQ To SQL
// will need it as a list (or array)
List<string> prefixes = GetListOfPrefixesFromSomewhere().ToList();

IQueryable<Record> query = GetAllRecordsFromRepository()
             .Where(x => !prefixes.Any(prefix => x.Field.StartsWith(prefix)));

Quite what the SQL will look like, I don't know - but I think it's logically what you want, which is usually a good start.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜