以下是我的查询摘录:
...
join p in dc.PODs on c.ID equals p.Consignment into pg
from pgg in pg.DefaultIfEmpty()
...
查询应该做的是获取与托运相关联的所有“POD”,将其存储为IEnumerable对象(似乎可以工作),这样我就可以在查询从查询生成的主要IEnumerable时运行它。
问题是,我正在使用DefaultIfEmpty行获得重复的主行,这只会在一行有多个POD时发生 - 所以它为每个POD返回一行,这是不正确的。如果我取出pg.DefaultIfEmpty()行,似乎可以更好地工作,但我仍然希望得到没有POD的行。
任何想法的人?
答案 0 :(得分:1)
只想确认你的第二种情况,输出是否没有没有项目的Two,Four,Five,因为它不是外连接?
One
1 TextBox
Three
3 Refridgerator
3 Bucket
我尝试过使用等效的WHERE IN来实现dc.PODS。
....join appraisal in ef_appraisal on application.a_appraisalid equals appraisal.a_appraisalid
where
(from r in ..
select r.r_applicationid).Contains(application.a_id) )
如果您有其他想法,请分享
答案 1 :(得分:0)
请原谅我,如果我的意图不合适,因为我无法在您的查询摘录中看到您的数据的完整结构或最初的from
或最终select
条款。因此,我根据您构建的代码段和示例数据发布了我认为的解决方案。如果我离开,请告诉我,我会纠正它。
如果你想要POD的托运行列表,每个托运到POD自己的行,你可以做这样的事情(请记住我的from
和select
条款是基于在我的样本数据上):
// select the consignment id & name (i made up) and each matching POD
var results = from c in consignments
join p in dc.PODs on c.ID equals p.Consignment into pg
from pgg in pg.DefaultIfEmpty()
select new { ID = c.ID, Name = c.Name, POD = pgg };
// This is just a SAMPLE display just for kicks and grins
foreach (var r in results)
{
Console.WriteLine(r.Name + " " + ((r.POD != null)
? (r.POD.Consignment + " " + r.POD.Description)
: "none"));
}
此查询输出如下内容:
One 1 TextBox
Two none
Three 3 Refridgerator
Three 3 Bucket
Four none
Five none
但是我不太清楚我理解你的评论:
“问题是,我得到了 重复主行“
我不确定你是否说你不希望看到每行每次购买一件货物,IEnumerable
中的每件产品都是带有托运货物的物品和一系列POD,你我想要一个像这样的查询:
// select the Consignment ID and Name (i made up), and list of PODs
// instead of the individual POD
var results = from c in consignments
join p in dc.PODs on c.ID equals p.Consignment into pg
select new { ID = c.ID, Name = c.Name, PODs = pg };
// This is just a SAMPLE display just for kicks and grins
foreach (var r in results)
{
Console.WriteLine(r.Name + " ");
if (r.PODs.Count() > 0)
{
foreach (var pod in r.PODs)
{
Console.WriteLine("\t" + pod.Consignment + " " + pod.Description);
}
}
else
{
Console.WriteLine("\tNone");
}
}
选择选择POD列表而不是单个匹配,其输出如下:
One
1 TextBox
Two
None
Three
3 Refridgerator
3 Bucket
Four
None
Five
None