m0sa
m0sa

Reputation: 10940

EF4 linq group item indexing

I have the following ER model:

Entry: Id, UserId, .... Fact: Id, EntryId, GroupId, DataType, DataValue ...

I have the following linq query to the EF4 data context:

var linq = 
 from entry in DataContext.Entry
 where entry.UserId== User.Identity.UserId
 from fact in entry.Facts
 group fact by new { fact.Entry, fact.GroupId } into g
 select g;

I want to assign a index to each of the groups like this:

linq = linq.Select((Group,Index) => new {Group, Index})

but I get a System.NotSupportedException. Is there any other way to achieve this in linq to ef?

I don't want to resort to linq to objects (e.g. by calling linq = linq.ToList()), since I extend the query further down in the code and want it to execute in 1 sql command.

Upvotes: 1

Views: 464

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

Linq to EF cannot use row indexing (select with indexes is not supported). You must do it in linq to objects.

var query = // your grouping query;
var linq = query.AsEnumerable().Select((Group,Index) => new {Group, Index});

Linq-to-entities supports row indexing internally only if you use Take() and Skip() methods on ordered queryable but still you can't use row index in the query.

Upvotes: 3

Related Questions