Linq语法 - 选择多个列 [英] Linq Syntax - Selecting multiple columns
问题描述
这是我用于我的实体模型的Linq语法
This is my Linq Syntax which I am using to my entity model
IQueryable<string> objEmployee = null;
objEmployee = from res in _db.EMPLOYEEs
where (res.EMAIL == givenInfo || res.USER_NAME == givenInfo)
select res.EMAIL;
如何选择多列?就像我想选择res.ID一样。那我该怎么收呢? IQueryable将无法正常工作。
这被称为Linq to SQL - 对吗?
How can I select multiple columns? Like I want to select res.ID aswell. And how can I receive those? IQueryable will not work I think. And this is called Linq to SQL - right ?
推荐答案
正如其他答案所示,您需要使用一个匿名类型。
As the other answers have indicated, you need to use an anonymous type.
就语法而言,我个人更喜欢方法链接。链接等价的方法是: -
As far as syntax is concerned, I personally far prefer method chaining. The method chaining equivalent would be:-
var employee = _db.EMPLOYEEs
.Where(x => x.EMAIL == givenInfo || x.USER_NAME == givenInfo)
.Select(x => new { x.EMAIL, x.ID });
AFAIK,声明式LINQ语法在编译时转换为与此类似的方法调用链。
AFAIK, the declarative LINQ syntax is converted to a method call chain similar to this when it is compiled.
更新
如果你想要整个对象,那么你只需要省略对 Select()
的调用,即
If you want the entire object, then you just have to omit the call to Select()
, i.e.
var employee = _db.EMPLOYEEs
.Where(x => x.EMAIL == givenInfo || x.USER_NAME == givenInfo);
这篇关于Linq语法 - 选择多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!