Returning a list using select new with LINQ

7

I can do this:

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>();

No select new contains all attributes of class vw_filial . But, Visual Studio returns me the following error:

  

'System.Linq.IQueryable' does not contain a setting   to 'ToList' and that the 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 get the expression return, and store it in a list of type vw_filiais .

    
asked by anonymous 18.12.2014 / 12:47

2 answers

10

No select new pass the type of 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();
    
18.12.2014 / 13:02
1

In this case the simplest would look like this:

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

Or inline like this:

var filial = base.EntityContext.vw_filial.ToList();
    
25.01.2015 / 02:43