Querying against multiple columns in Entity Framework 6

c# entity-framework-6 linq sql wpf

Question

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?

1
1
1/13/2017 9:35:07 PM

Accepted Answer

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

3
2/17/2014 6:22:35 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow