wpf 查询实体框架 6 中的多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21814662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:53:03  来源:igfitidea点击:

Querying against multiple columns in Entity Framework 6

c#sqlwpflinqentity-framework-6

提问by UnbiasedMilo

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".

我正在尝试对 EF 6 中的多个列执行查询(在 VS 2013 上使用 C# WPF),我需要选择某些列的所有字段。我真的不太了解,但我已经尝试用 Linq 来做它,它似乎没有那个功能,所以我去了<context>.Database.SqlQuery<string>(query),但我不清楚我应该如何处理它返回的内容。查询很简单,例如"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?

有可能做到Linq吗?如何?对于这种SqlQuery()情况,我应该如何处理它的结果,因为它的大部分列都是字符串格式?

回答by Harshana Narangoda

@MiloGP yes you can do it with using Lambda Expression with LINQ

@MiloGP 是的,您可以通过在 LINQ 中使用 Lambda 表达式来实现

Here a example:

这里有一个例子:

I have 5 columns in table employee(emp_id,emp_name,emp_dob,emp_address,emp_reference)

我在表员工(emp_id,emp_name,emp_dob,emp_address,emp_reference)中有 5 列

and My DBContext name : EmployeeEntities;

和我的 DBContext 名称:EmployeeEntities;

I trying to get emp_name and emp_address

我试图获得 emp_name 和 emp_address

List<employee> = EmployeeEntities.employees.select( x => new { x.emp_name, x.emp_address }).ToList();

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

如果您需要获取某人的价值,例如 emp_id == 13458

List<employee> = EmployeeEntities.employees.Select( x => new { x.emp_name, x.emp_address }).Where( y => y.emp_id == 13458).ToList();

List<employee> = EmployeeEntities.employees.Select( x => new { x.emp_name, x.emp_address }).Where( y => y.emp_id == 13458).ToList();