Linq 语法 - 选择多列 [英] Linq Syntax - Selecting multiple columns

查看:34
本文介绍了Linq 语法 - 选择多列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用于实体模型的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆