Difference between these LINQ statements.
-
Saturday, August 18, 2012 4:32 AM
HI,
plz let me know hte difference between these LINQ statements..
var result = from emp in dsObj.Emp from ord in dsObj.Order where emp.ID == ord.EmpID select new { emp.ID, ord.OrdName };
var result1 = from emp in dsObj.Emp join ord in dsObj.Order on emp.ID equals ord.EmpID select new { emp .ID,ord.EmpID };
Thanks,
Sanjay.
All Replies
-
Saturday, August 18, 2012 9:26 AM
E.g:
Table1——Employee:
1)EmployeeId
2)EmployeeName
Table2——Orders
1)Id
2)name
3)EmployeeId(foreign key)
So link Table1's EmployeeId to Table2's EmployeeId (One-To-Many):
The first linq will be "Each record from Table1, if founds the same in Table2, output the result".
The second linq will be "Each record from table1, and combine the all the records from Table2".
So the first one is:
The second one:
Table2's groupped, so there are only three records left to be outputted. And u have to use a nested foreach to loop and output its sub items.
- Proposed As Answer by Sachin P Saturday, August 18, 2012 9:37 AM
- Marked As Answer by Jason Dot WangMicrosoft Contingent Staff, Moderator Friday, August 24, 2012 8:17 AM
-
Tuesday, September 25, 2012 9:47 AM
HI Thanks for your reply. Can you please explain the below line in a brief way..
"Table2's groupped, so there are only three records left to be outputted. And u have to use a nested foreach to loop and output its sub items." I could fidn the output result for both the quries are same..
sanjay
- Edited by msanjayv Tuesday, September 25, 2012 9:49 AM amended
-
Tuesday, September 25, 2012 10:07 AM
HI Thanks for your reply. Can you please explain the below line in a brief way..
"Table2's groupped, so there are only three records left to be outputted. And u have to use a nested foreach to loop and output its sub items." I could fidn the output result for both the quries are same..
sanjay
I mean that in fact,This method in C# has been translated into GroupJoin:
var result = from e in Employees join o in Orders on e.Id equal o.Id into temp select new { ………… }; //This will be translated into the Lambda Expression: var result = Employees.GroupJoin
(Orders,
e=>e.Id,
o=>o.Id,
(e,orders)=>
new {EmployeeId=e.Id,Orders = orders}
); -
Tuesday, September 25, 2012 11:43 AM
HI
Thanks. What I could not understand is
The first linq will be "Each record from Table1, if founds the same in Table2, output the result".
The second linq will be "Each record from table1, and combine the all the records from Table2". ( from your first post..).
plz explain.
Sanjay.
- Edited by msanjayv Tuesday, September 25, 2012 11:45 AM Amended.
-
Tuesday, September 25, 2012 3:33 PM
For the first case it will take each item in table1 and pair it with each item in table2. It will then filter the results that meet a certain condition.
The second case is a `join`, which is a sort of special case of the previous example. It will find all pairs of items between the two tables that match a certain condition, that condition being where the value of a particular field in each table matches.
The resulting data of both calls will be identical, as they are written. The difference is that by using `join` rather than selectmany followed by a where (that's what the first case is doing), it could potentially be more efficient. That particular special case allows for optimizations that can't happen if you must first generate all possible options and only filter after you've generated them. Using `join` you can know not to generate lots of the pairs in the first place knowing that they would later be discarded.
However, this isn't linq to objects, from the look of it. These appear to be `IQueryable` instances linking to some database. Because of this it will (probably) generate SQL, and that SQL will be compiled and optimized by a database. My guess is that the query provider, or if not that, then the database itself, will recognize the first pattern and determine that it actually is a join and perform the same optimization, so the end result should be that both are entirely identical. (Of course, they select a different field for the results, but that's not a particularly interesting difference.)
-
Tuesday, September 25, 2012 4:07 PM
However, this isn't linq to objects, from the look of it.
I don't see your clue.
From the name of dsObj, I would think it's a typed DataSet, and so Linq to objects.
-
Tuesday, September 25, 2012 4:26 PM
"I don't see your clue.
From the name of dsObj, I would think it's a typed DataSet, and so Linq to objects."If it is linq to objects then he can simply ignore my last paragraph. The results will be the same, but the first option will be quite a bit less efficient (especially for large data sets).
If these were data sets then field access would be different; he wouldn't be able to use implicit conversion and still have properties for each field. A dataset wouldn't have an `Emp` or an`Order` property either.
It just feels like an ORM to me.- Edited by servy42Microsoft Community Contributor Tuesday, September 25, 2012 4:27 PM
-
Wednesday, September 26, 2012 1:55 PM
If these were data sets then field access would be different; he wouldn't be able to use implicit conversion and still have properties for each field. A dataset wouldn't have an `Emp` or an`Order` property either.
Apparently, you never used typed datasets.

