Request a connection or use LINQ magic?

It is better to make a combined query as follows:

var employer = (from person in db.People
                  join employer in db.Employers
                  on person.EmployerID equals employer.EmployerID 
                  where person.PersonID == idPerson
                  select employer).FirstOrDefault();

Or just as well doing the easy thing and doing it (with zero checks):

var employer = (from person in db.People
                  where person.PersonID == idPerson
                  select person).FirstOrDefault().Employer;

Obviously, in this I would really have to do this in 2 statements in order to get a null check.

Is there any good practice for reading or performance issues?

+3
source share
3 answers

I would use this:

var employer = (from person in db.People
                where person.PersonID == idPerson
                select person.Employer).FirstOrDefault();

He got the simplicity of the first version, but still only retrieves data for the employer (and not the person and the employer).

+6
source

The second can be evaluated as null, which will lead to an error.

I like the first one better, because if it is zero, you can handle it without exception.

+1

, , .

LinqPad SQL, .

+1

Source: https://habr.com/ru/post/1698677/


All Articles