Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Select within a Select

Tags:

c#

linq

I am trying to query a collection that contains Employee information. When I query that collection I would like to return an enumeration of objects where each object has two fields:

  • Name
  • ManagerName

(Note that every Manager is also an Employee!)

Now, here's the problem I am having. When I do a select within a select, the value of the ManagerName field that is returned on each object is:

System.Data.Common.Internal.Materialization.CompensatingCollection<string>

Here's the query:

var query =
    from e in db.Employees    
    select new
    {
        Name = e.Name,
        ManagerName =
            from em2 in db.Employees
            where (em2.EmployeeID == e.ManagerID)
            select em2.Name
    };

Specifically, when I look at the value of ManagerName, I see that it is an enumeration that yields a single item. And that the single item is a string that contains the name of the Manager. So, I think I'm close.

Question: How can I change my query so that instead it returns an enumeration of objects where each object simply has two string fields, Name and ManagerName?

like image 533
DK ALT Avatar asked Jul 24 '13 17:07

DK ALT


People also ask

What does select in LINQ do?

The Select() method invokes the provided selector delegate on each element of the source IEnumerable<T> sequence, and returns a new result IEnumerable<U> sequence containing the output of each invocation.

How to use select query in LINQ?

The select query in LINQ to SQL is used to get all the records or rows from the table. LINQ to SQL select query can be used to filter the records of the table with the where clause. Here, we can also perform multiple operations like grouping, joining, etc. using LINQ to SQL select query based on our requirement.

What is .select C#?

In a query expression, the select clause specifies the type of values that will be produced when the query is executed. The result is based on the evaluation of all the previous clauses and on any expressions in the select clause itself. A query expression must terminate with either a select clause or a group clause.

How to use in LINQ query C#?

In a LINQ query, the first step is to specify the data source. In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from clause comes first in order to introduce the data source ( customers ) and the range variable ( cust ).


2 Answers

Try this:

var query = from e in db.Employees
            select new
            {
                Name = e.Name,
                ManagerName = db.Employees
                                .Where(x => x.EmployeeID == e.ManagerID)
                                .Select(x => x.Name).SingleOrDefault()
            };

However, if you correctly mapped your database with EF (which I suppose you are using), you should have a navigation property you can utilize:

var query = from e in db.Employees
            select new
            {
                Name = e.Name,
                ManagerName = e.Manager.Name
            };
like image 95
Daniel Hilgarth Avatar answered Oct 05 '22 23:10

Daniel Hilgarth


Looks like a self-join should work:

var query = from e in db.Employees
            join m in db.Employees on e.ManagerID equals m.EmployeeID
              select new
              {
                Name = e.Name,
                ManagerName = m.Name
              };
like image 22
D Stanley Avatar answered Oct 05 '22 23:10

D Stanley