Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq error - "NotSupportedException: Unsupported overload used for query operator 'Select'"

Tags:

linq

I have the following Linq query:

var tmp = 
    from container in Container
    join containerType in ContainerType on container.ContainerType equals containerType
    where containerType.ContainerTypeID == 2
    select new { ContainerID = container.ContainerID, TypeID = container.ContainerTypeID};

var results = tmp.Select((row, index) => new { row.ContainerID, row.TypeID, ContainerIndex = index })

As is, this works fine. If I add the following, so I can see the results in LinqPad, I get the error described in the title of this message:

results.Dump();

This error is not a LinqPad error, it's coming from Linq, and I don't understand what it means.

Thank you.

like image 892
Randy Minder Avatar asked Jul 07 '10 12:07

Randy Minder


1 Answers

Okay, I hadn't realised Container was a LINQ to SQL data source to start with. Basically it's failing to convert the second projection to SQL.

So, you want to do just that bit in .NET instead - you can force it to use Enumerable.Select with AsEnumerable:

var results = tmp.AsEnumerable()
                 .Select((row, index) => new { row.ContainerID, row.TypeID,
                                               ContainerIndex = index });
like image 50
Jon Skeet Avatar answered Oct 17 '22 21:10

Jon Skeet