The method ElementAt(i) is not recognized by LINQ to Entities.

c# entity entity-framework linq

Question

I'm using the method elementat(Int32) to 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 am I getting this error and how can I fix it?

1
15
8/16/2017 8:42:46 PM

Accepted Answer

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?

22
2/26/2010 2:20:28 PM

Popular Answer

You can simply mix Skip and First to do the trick:

mds.Skip(i).First()


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow