Returning a list using select new with LINQ

Asked

Viewed 1,398 times

7

I can do it:

var filial = (from f in base.EntityContext.vw_filial
              select f).ToList<vw_filial>();

But I want to do something like this:

var filial = (from f in base.EntityContext.vw_filial
     select new
     {
         COD_FILIAL = f.COD_FILIAL,
         CGCCPF = f.CGCCPF,
         NM_FILIAL = f.NM_FILIAL,
         NMC_FILIAL = f.NMC_FILIAL,
         END_FILIAL = f.END_FILIAL,
         BAI_FILIAL = f.BAI_FILIAL,
         CEP_FILIAL = f.CEP_FILIAL,
         CID_FILIAL = f.CID_FILIAL,
         UF_FILIAL = f.UF_FILIAL
     }).ToList<vw_filial>();

In the select new contains all class attributes vw_filial. But, the visual Studio returns me the following error:

'System.Linq.Iqueryable' does not contain a definition for 'Tolist' and that method 'System.Linq.Parallelenumerable.Tolist(System.Linq.Parallelquery)' has some wrong arguments. 'System.Linq.Iqueryable' does not contain a definiton for 'Tolist' and the best Extension method Overload 'System.Linq.Parallelenumerable.Tolist(System.Linq.Parallelquery)' has some invalid Arguments.

What I need is to take the return of the expression, and store it on a list of the kind vw_filiais.

2 answers

10


In the select new pass the type of the object you want to return in the list.

var filial = (from f in base.EntityContext.vw_filial
     select new vw_filial
     {
         COD_FILIAL = f.COD_FILIAL,
         CGCCPF = f.CGCCPF,
         NM_FILIAL = f.NM_FILIAL,
         NMC_FILIAL = f.NMC_FILIAL,
         END_FILIAL = f.END_FILIAL,
         BAI_FILIAL = f.BAI_FILIAL,
         CEP_FILIAL = f.CEP_FILIAL,
         CID_FILIAL = f.CID_FILIAL,
         UF_FILIAL = f.UF_FILIAL
     }).ToList();
  • 1

    You really solved my problem.

  • And if select returns more than one table, I mean when it does a Join, which object would it return ?? because it would have a mixture of entities there.

1

In that case the simplest would be:

var filial = (from f in base.EntityContext.vw_filial
     select f).ToList();

Or inline like this:

var filial = base.EntityContext.vw_filial.ToList();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.