Multiple column selection using Linq syntax

c# entity-framework linq

Question

I use this Linq syntax for creating my entity model.

IQueryable<string> objEmployee = null;

objEmployee = from res in _db.EMPLOYEEs
              where (res.EMAIL == givenInfo || res.USER_NAME == givenInfo)
              select res.EMAIL;

How can I choose many columns? Similarly, I want to choose res.ID as well. How can I get them, then? I don't believe IQueryable will function. And this is called Linq to SQL - right ?

1
93
8/9/2017 7:31:49 AM

Accepted Answer

You must use an anonymous type, as stated in the previous responses.

In terms of syntax, method chaining is by far my preference. The equivalent of method chaining would be:

var employee = _db.EMPLOYEEs
    .Where(x => x.EMAIL == givenInfo || x.USER_NAME == givenInfo)
    .Select(x => new { x.EMAIL, x.ID });

As far as I know, when declarative LINQ syntax is compiled, it is transformed into a method call chain that like this.

UPDATE

Simply skip the call to if you only want the complete object.Select() , i.e.

var employee = _db.EMPLOYEEs
    .Where(x => x.EMAIL == givenInfo || x.USER_NAME == givenInfo);
182
7/21/2011 6:18:13 PM

Popular Answer

Use anonymous types, for instance:

  var empData = from res in _db.EMPLOYEEs
                where res.EMAIL == givenInfo || res.USER_NAME == givenInfo
                select new { res.EMAIL, res.USER_NAME };


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