Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine these two linq queries into a single query?

Tags:

c#

linq

How can I get this in to one query? What I want is the Person from the Company that matches the name of the person I'm searching for.

Currently I get the Company and then run basically the same search.

var existingCompany = bidInfo.Companies
        .FirstOrDefault( c=> c.CompanyDomain != null && 
                           c.CompanyDomain.People.FirstOrDefault( 
                                 p => p.Name == bidInfo.ArchitectPerson.Name ) 
                                        != null);
Person existingPerson=null;
if (existingCompany !=null)
{
     existingPerson = existingCompany.CompanyDomain.People.FirstOrDefault(p => p.Name == bidInfo.ArchitectPerson.Name);
}
like image 511
T McKeown Avatar asked Apr 22 '16 02:04

T McKeown


People also ask

How you can merge the results from two separate LINQ queries into a single result set?

CORRECT ANSWER : Use the Concat method.

How LINQ queries converted into SQL queries?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

Can we do Joins in LINQ?

In LINQ, an inner join is used to serve a result which contains only those elements from the first data source that appears only one time in the second data source. And if an element of the first data source does not have matching elements, then it will not appear in the result data set.

What are LINQ joins?

Join Method (System. Linq) Correlates the elements of two sequences based on matching keys.


2 Answers

Assuming I understand what you're trying to do, you could do something like this:

var person = bidInfo.Companies
            .Where(c=>c.CompanyDomain != null)
            .SelectMany(c=>c.CompanyDomain.People)
            .SingleOrDefault(p=>p.Name == bidInfo.ArchitectPerson.Name);

Note that you're already not filtering on the company (you're just getting the first company that has someone with that name, what if there are multiples? If that's not possible then the company check is useless and you may as well do as I do and just select all people then filter on that instead of going inside of each company, checking if the person is there, then somehow going up and back down!)

like image 196
Ronan Thibaudau Avatar answered Nov 01 '22 05:11

Ronan Thibaudau


In order to find the Person from the Company that matches the name of the bidInfo.ArchitectPerson you're searching for, you will need to look at all the people in the companies associated with the bidInfo, and then find the person with the matching name.

This can be accomplished with the following:

var existingPerson = bidInfo.Companies
    .Where(c => c.CompanyDomain != null && c.CompanyDomain.People != null)
    .SelectMany(c => c.CompanyDomain.People)
    .FirstOrDefault(p => p.Name == bidInfo.ArchitectPerson.Name)


FirstOrDefault vs. SingleOrDefault:

Names are unlikely to be unique: one company could have more than one "John Smith" working for them; one bidInfo could contain several Companies, more than one of whom each employ a different "Jane Smith".

SingleOrDefault() will throw an exception if there is more than one element that matches the p.Name == bidInfo.ArchitectPerson.Name criterion.

So long as the "Smith cases" outlined above describe an acceptable state for your program, use FirstOrDefault().

like image 5
AGB Avatar answered Nov 01 '22 04:11

AGB