views:

745

answers:

2

Hi, i'm using the method elementat for get a specific element of a query's result.

var mds = db.TDP_MissioniDestinazioni.Where(p => p.MissioneID == missioneRow.MissioneID);  



destinazioneRow = mds.ElementAt(i);

LINQ to Entities does not recognize the method 'TimeEntModel.TDP_MissioniDestinazioni ElementAt[TDP_MissioniDestinazioni](System.Linq.IQueryable`1[TimeEntModel.TDP_MissioniDestinazioni], Int32)' method, and this method cannot be translated into a store expression.

Why?

how can i fix it? thanks

+3  A: 

Are you happy to fetch all the "earlier" results? If so, either call ToList() to cache them, or AsEnumerable() to fetch them on each call, with the AsEnumerable just being a way to force the compiler to call Enumerable.ElementAt instead of Queryable.ElementAt.

There may be a better way (e.g. using Take or Skip) though - could you give more information about the bigger picture?

Jon Skeet
+1  A: 

You could use:

mds.Skip(i-1).Take(1).Single()

Or the easier one (thanks Jon):

mds.Skip(i-1).First()
Steven
@Steven: Any idea what the translation for that would be like compared with mds.Skip(i - 1).First()?
Jon Skeet
@Jon: Hopefully EF would generate the same SQL code for this, but my educated guess is that your `mds.Skip(i-1).First()` would produce more efficient SQL. Good one!
Steven