Reputation: 1018
I am trying to achieve a simple task, and I don't find a way to do it. I am trying to convert a LINQ result to a list of objects. I am following the syntax posted in this accepted SO answer.. Here is my code:
var LinqHallazgos = (from RA_Hallazgos in context.RA_Hallazgos
select new
{
HallazgoId = RA_Hallazgos.HallazgoId,
Hallazgo = RA_Hallazgos.Hallazgo
});
List<RA_Hallazgos> Hallazgos = LinqHallazgos.ToList<RA_Hallazgos>();
And this is the error that I get:
It says that "LinqHallazgos", which is an iqueryable, doesn't contain a definition for ToList. But I see the "ToList" method in intellisense. It also says that it expect an IEnumerable but this answer seems to use similar syntax.
What I am doing wrong and how in the world I get the LINQ result as a list of a particular object??
Upvotes: 0
Views: 860
Reputation: 62213
You are making this too complex. It seems all you want to do is materialize the DbSet<RA_Hallazgos>
to memory (retrieve the content from the database). If that is the case just use the ToList()
linq extension on the DbSet
and you are done. Note that it will retrieve everything as you did not specify a filter (ie. any limiting predicates like Where / Take / Skip / etc).
List<RA_Hallazgos> Hallazgos = context.RA_Hallazgos.ToList();
Upvotes: 2
Reputation: 29981
Just call .ToList()
. You have an anonymous type there (the new {}
) so you can't specify the type explicitly even if you wanted to.
Otherwise, if RA_Hallazgos
is an actual type, use new RA_Hallazgos{}
to avoid creating an anonymous type.
Upvotes: 1
Reputation: 15663
Try
select new RA_Hallazgos
{
HallazgoId = RA_Hallazgos.HallazgoId,
Hallazgo = RA_Hallazgos.Hallazgo
}
List<RA_Hallazgos> Hallazgos = LinqHallazgos.ToList()();
Upvotes: 1