开发者

In Linq to EF 4.0, I want to return rows matching a list or all rows if the list is empty. How do I do this in an elegant way?

开发者 https://www.devze.com 2023-04-09 10:15 出处:网络
This sort of thing: Dim MatchingValues() As Integer = {5, 6, 7} Return From e in context.entity Where MatchingValues.Contains(e.Id)

This sort of thing:

Dim MatchingValues() As Integer = {5, 6, 7}
Return From e in context.entity
       Where MatchingValues.Contains(e.Id)

...works great. However, in my case, the values in MatchingValues are provided by the user. If none are provided, all rows ought to be returned. It would be wonderful if I could do this:

Return From e in context.entity
       Where (MatchingValues.Length = 0) OrElse (MatchingValues.Contains(e.Id))

Alas, the array length test cannot be converted to SQL. I could, of course, code this:

If MatchingValues.Length = 0 Then
   Return From e in context.entity
Else
   Return From e in context.entity
          Where MatchingValues.Contains(e.Id)
End If

This solution doesn't scale well. My application needs to work with 5 such lists, which means I'd need to code 32 queries, one for every situation.

I could also fill MatchingValues with every existing value when the user doesn't want to use the filter. However, there could be thousands of values in each of the five 开发者_高级运维lists. Again, that's not optimal.

There must be a better way. Ideas?


Give this a try: (Sorry for the C# code, but you get the idea)

IQueryable<T> query = context.Entity;
if (matchingValues.Length < 0) {
    query = query.Where(e => matchingValues.Contains(e.Id));
}

You could do this with the other lists aswell.

0

精彩评论

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