Abhilash J A

Abhilash J A

  • NA
  • 2.4k
  • 582.5k

how to select two columns from DBContext linq c#?

Feb 17 2017 3:55 AM
Hello Everyone,
 
How can I select only two columns from the table via linq from DBContext ?
 
I have tried,
 
Here run time error occurred. 
  1. public List<PartnerOffice> ShowOfficesList()  
  2.         {  
  3.             using (var context = new ReboxEntities())  
  4.             {  
  5.                 var listPartnerOfficeName = from offices in context.PartnerOffices select new PartnerOffice() { idPartnerOffice = offices.idPartnerOffice, Officename = offices.Officename };  
  6.                 return listPartnerOfficeName.ToList<PartnerOffice>();  
  7.             }  
  8.         } 
 I have tried this also,
 
Here compile time error occurred.
  1. public List<PartnerOffice> ShowOfficesList()  
  2.        {  
  3.            using (var context = new ReboxEntities())  
  4.            {  
  5.                return context.PartnerOffices.Select(c => new { c.idPartnerOffice, c.Officename }).ToList();                 
  6.            }  
  7.        } 
 Please help me...

Answers (2)