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);
}
CORRECT ANSWER : Use the Concat method.
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.
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.
Join Method (System. Linq) Correlates the elements of two sequences based on matching keys.
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!)
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With