问题
I have the source code like this:
var res = from s in Splitting
join c in Customer on s.CustomerId equals c.Id
where c.Id == customrId
&& c.CompanyId == companyId
select s;
When reviewing code, one member said that my code applies for only one SQL db, and advised me to use LinQ to Entity with Join so that it will work for all databases.
I don't understand, I think, even with other db, we will add it to Entity Framework. And the code below will work correct too, right?
Please advise.
回答1:
There are two ways you can write your LINQ.
1.LINQ Query Expressions (query-syntax) (Which you have done)
var res = from s in Splitting
join c in Customer on s.CustomerId equals c.Id
where c.Id == customrId
&& c.CompanyId == companyId
select s;
2.Another is LINQ query extension methods (dot-syntax)
var res = Splitting.Join(Customer,
sp => sp.CustomerId,
cu => cu.Id,
(sp, cu) => new { sp, cu })
.Where(s => s.cu.Id == customrId && s.cu.CompanyId == companId)
.Select(s => s.sp);
For joins, I strongly prefer query-syntax.There are details that query-syntax hides that can make it well worth embracing with the improvement to readability it brings.However query-syntax is somewhat more limited than dot-syntax in other aspects.
dot-syntax is more concise but performing multiple table joins is a nightmare.The flip side is that there are a number of LINQ operations that only exist within the dot-syntax: Single(), First(), Count() etc.For these limitation of query-syntax you can use dot-syntax.
N.B : At compile time, all are converted to Standard Query.
来源:https://stackoverflow.com/questions/43579030/linq-to-sql-and-where-is-the-choice-for-all-database