I'm trying to execute a query against multiple columns in EF 6 (using C# WPF on VS 2013), I need to select all fields of some columns. I really don't know much but I already tried to do it with Linq and it doesn't seem to have that functionality, so I went with <context>.Database.SqlQuery<string>(query)
, but it's unclear to me how should I handle what it returns. The query is something simple like "SELECT column1,column2 FROM table"
.
Is it possible to do it with Linq
? How? And for the SqlQuery()
case, How should I handle it's result, being most of its columns are in string format?
@MiloGP yes you can do it with using Lambda Expression with LINQ
Here a example:
I have 5 columns in table employee(emp_id,emp_name,emp_dob,emp_address,emp_reference)
and My DBContext name : EmployeeEntities;
I trying to get emp_name and emp_address
List<employee> = EmployeeEntities.employees.select( x => new { x.emp_name, x.emp_address }).ToList();
if you need to get value of someone, As a example emp_id == 13458
List<employee> = EmployeeEntities.employees.Select( x => new { x.emp_name, x.emp_address }).Where( y => y.emp_id == 13458).ToList();