0

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:

enter image description here

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??

Henk Holterman
  • 236,989
  • 28
  • 287
  • 464
SamyCode
  • 608
  • 2
  • 8
  • 26

3 Answers3

2

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();
Igor
  • 55,253
  • 10
  • 80
  • 149
1

Try

select new RA_Hallazgos
{
    HallazgoId = RA_Hallazgos.HallazgoId,
    Hallazgo = RA_Hallazgos.Hallazgo
}
List<RA_Hallazgos> Hallazgos = LinqHallazgos.ToList()();
Adrian Iftode
  • 14,740
  • 3
  • 41
  • 71
1

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.

Cory Nelson
  • 26,928
  • 5
  • 69
  • 103