Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack overflow in LINQ to SQL and the Contains keyword

I have an Extension method which is supposed to filter a Queryable object (IQueryable) based upon a collection of Ids....

Note that IQueryable is sourced from my database via a LinqToSql request

 public static IQueryable<NewsItemSummary> WithID(this IQueryable<NewsItemSummary> qry, IQueryable<Guid> Ids)
    {
        return from newsItemSummary in qry
               where Ids.Contains(newsItemSummary.ID)
               select newsItemSummary;
    }

If Ids are created from an array or list and passed in as a queryable list, it DOESNT work

For example...

 GetNewsItemSummary().WithID(ids.AsQueryable<Guid>())

If Ids is composed form a LinqToSql request, it DOES work!!

This is known issue: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=355026

My Ids collection cannot come from a LinqToSql request...

Note, if I change the function so that it consumes and IList rather than an IQueryable....

 public static IQueryable<NewsItemSummary> WithID(this IQueryable<NewsItemSummary> qry, IList<Guid> Ids)
    {
        return from newsItemSummary in qry
               where Ids.Contains(newsItemSummary.ID)
               select newsItemSummary;
    }

I now get the following exception:

Method 'Boolean Contains(System.Guid)' has no supported translation to SQL.

So... all I want to do is filter my collection of news based upon a list or array of Guids.... Ideas???

like image 931
iasksillyquestions Avatar asked Jun 07 '09 13:06

iasksillyquestions


People also ask

How use contains in LINQ?

The Linq Contains Method in C# is used to check whether a sequence or collection (i.e. data source) contains a specified element or not. If the data source contains the specified element, then it returns true else return false.

What is the difference between contains and any in LINQ?

Contains takes an object, Any takes a predicate. So if you want to check for a specific condition, use Any . If you want to check for the existence of an element, use Contains .

What is the use of into keyword in LINQ?

The into contextual keyword can be used to create a temporary identifier to store the results of a group, join or select clause into a new identifier. This identifier can itself be a generator for additional query commands.

Is LINQ to SQL deprecated?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.


1 Answers

This will translate.

public static IQueryable<NewsItemSummary> WithID(
    this IQueryable<NewsItemSummary> qry,
    List<Guid> Ids
)
    {
        return from newsItemSummary in qry
               where Ids.Contains(newsItemSummary.ID)
               select newsItemSummary;
    }
)

Translation of the Contains method against local collections was one of the last features added in the development of linq to sql for .net 3.5, so there are some cases that you would expect work that don't - such as translation of IList<T>.

Also, be aware that while LinqToSql will happily translate lists containing a vast number of items (I've seen it do over 50,000 elements), SQL Server will only accept 2,100 parameters for a single query.

like image 58
Amy B Avatar answered Nov 12 '22 14:11

Amy B