我在数据库中有一个查询:
SELECT GreenInventoryBlendGradeID,bgx.blendgradeid,
bgX.GreenBlendGradeTypeID,[Description]
FROM [GreenInventory] gi
INNER JOIN [GreenInventoryBlendGradeXref] bgX
ON bgX.[GreenInventoryID] = gi.[GreenInventoryID]
INNER JOIN [BlendGrade] bg
ON bg.[BlendGradeID]=bgx.[BlendGradeID]
这将返回 3 条记录:
类型 ID 描述
1 XR
2 XR
1 XF2
LINQ:
var GreenInventory = (from g in Session.GreenInventory
.Include("GreenInventoryBlendGradeXref")
.Include("GreenInventoryBlendGradeXref.BlendGrade")
.Include("GreenInventoryBlendGradeXref.GreenBlendGradeType")
.Include("GreenInventoryWeightXref")
.Where(x => x.GreenInventoryID == id && x.GreenInventoryBlendGradeXref.Any(bg=>bg.GreenBlendGradeTypeID > 0) )
select g);
我尝试了不同的 Where 子句,包括简单的 - (x => x.GreenInventoryID == id) 但总是只返回前 2 条记录。
有任何想法吗?
如果我尝试以下操作:
var GreenInventory = (from gi in Session.GreenInventory.Where(y => y.GreenInventoryID == id)
join bgX in Session.GreenInventoryBlendGradeXref.DefaultIfEmpty() on gi.GreenInventoryID equals bgX.GreenInventoryID
join bg in Session.BlendGrade.DefaultIfEmpty() on bgX.BlendGradeID equals g.BlendGradeID
select new { GreenInventory = gi, GreenInventoryBlendGradeXref = bgX, BlendGrade = bg });
我取回每个对象的 3 个,正确的信息在 BlendGrade 对象中。看起来 3 个 GreenInventory 对象是相同的。它们每个都包括 2 个 GreenInventoryBlendGradeXref 对象,它们显示与以前相同的 2 条记录。
所以我不清楚最初的问题是什么。也不知道这是否是解决它的最佳方法。
感谢您的回答。如果有人有进一步的想法,请告诉我们。