Reputation: 23
The query below works just fine and returns one List<>
exactly as expected
List<purchaseOrderHeaderEntity> details = (from p in
db.purchaseOrderDetailEntity join r in db.purchaseOrderHeaderEntity on
p.purchaseOrderID equals
r.purchaseOrderID
where p.productID == productID select r).ToList();
If I rewrite it using lambda, it returns two lists:
List<purchaseOrderHeaderEntity> lambda_details = db.purchaseOrderDetailEntity.Join(db.purchaseOrderHeaderEntity,
p => p.purchaseOrderID,
r => r.purchaseOrderID,
(p, r) => new { Detail = p, Header = r }).Where(DetailAndHeader => DetailAndHeader.Detail.productID == productID).ToList();
If I remove "Detail=p
" from the selection part, Join throws an error
"The type arguments for method ... cannot be inferred from the usage. Try specifying the type arguments explicitly."
Is there a way to rewrite that query to make it return just one list for Header?
Thanks a lot in advance!
Upvotes: 1
Views: 272
Reputation: 660024
Your query comprehension and your fluent query are different queries.
Your query comprehension expression is:
from p in db.purchaseOrderDetailEntity
join r in db.purchaseOrderHeaderEntity on
p.purchaseOrderID equals r.purchaseOrderID
where p.productID == productID
select r
Your fluent form is:
db.purchaseOrderDetailEntity.
Join(
db.purchaseOrderHeaderEntity,
p => p.purchaseOrderID,
r => r.purchaseOrderID,
(p, r) => new { p, r }).
Where(pr => pr.p.productID == productID)
But the correct fluent form is
db.purchaseOrderDetailEntity.
Join(
db.purchaseOrderHeaderEntity,
p => p.purchaseOrderID,
r => r.purchaseOrderID,
(p, r) => new { p, r }).
Where(pr => pr.p.productID == productID).
Select(pr => pr.r)
Does that answer your question? If not, please give a small program that we can compile and run that clearly reproduces the problem.
Upvotes: 2
Reputation: 2881
you can put the where condition before join, then you can return the r.
var lambda_details = db.purchaseOrderDetailEntity
.Where(detailAndHeader => detailAndHeader.Detail.productID == productID)
.Join(db.purchaseOrderHeaderEntity,
p => p.purchaseOrderID,
r => r.purchaseOrderID,
(p, r) => r).ToList();
Upvotes: 3