LINQ:结合加入和组 [英] LINQ: combining join and group by
问题描述
我有结合连接的查询和一组,但我有一个问题。查询是这样的:
VAR的结果=从产品
p加入BaseProducts英国石油公司p.BaseProductId等于基点.ID
群p由p.SomeId成PG
选择新ProductPriceMinMax {
SomeId = pg.FirstOrDefault()。SomeId,
COUNTRYCODE = pg.FirstOrDefault()。COUNTRYCODE,
MinPrice = pg.Min(M = GT; m.Price),
MaxPrice = pg.Max(M = GT; m.Price),
BaseProductName = bp.Name< - -----不是可以使用BP。
};
正如你所见,它加入与BaseProducts表中的Products表和组上的一个id产品表。但在所得ProductPriceMinMax,我还需要BaseProducts表的属性:bp.Name,但是它不知道碱基。
任何想法,我做错了吗?
谢谢!
< DIV CLASS =h2_lin>解决方案
一旦你做到了这一点。
p组为p .SomeId成PG
你不再有机会获得在最初的<$ C $使用的范围变量C>从。也就是说,你可以不再谈 P
或 BP
,你只能说说皮克
。
现在,皮克
为组所以包含的多个的产品。所有的产品在给定的皮克
组具有相同的 SomeId
(因为这就是你通过分组),但我不知道,如果这意味着他们都具有相同的 BaseProductId
。
要得到一个基本产品的名称,你有挑皮克
组中某一特定产品(如你与 SomeId
和 COUNTRYCODE做
)和然后的加盟 BaseProducts
。
VAR的结果=从产品
群p p由p.SomeId成PG
//加盟* *后组
在BaseProducts加入英国石油公司pg.FirstOrDefault()。BaseProductId等于bp.Id
选择新ProductPriceMinMax {
SomeId = pg.FirstOrDefault()。SomeId,
COUNTRYCODE = pg.FirstOrDefault()COUNTRYCODE,
MinPrice = pg.Min(M = GT; m.Price),
MaxPrice = pg.Max(M = GT; m.Price),
BaseProductName = bp.Name //现在有范围
}一个'基点';
这是说,这看起来非常不寻常的,我觉得你应该退一步考虑一下你实际上是试图检索。
I have a query that combines a join and a group, but I have a problem. The query is like:
var result = from p in Products
join bp in BaseProducts on p.BaseProductId equals bp.Id
group p by p.SomeId into pg
select new ProductPriceMinMax {
SomeId = pg.FirstOrDefault().SomeId,
CountryCode = pg.FirstOrDefault().CountryCode,
MinPrice = pg.Min(m => m.Price),
MaxPrice = pg.Max(m => m.Price),
BaseProductName = bp.Name <------ can't use bp.
};
As you see, it joins the Products table with the BaseProducts table, and groups on an id of the Product table. But in the resulting ProductPriceMinMax, I also need a property of the BaseProducts table: bp.Name, but it doesn't know bp.
Any idea what I'm doing wrong?
Thanks!
Once you've done this
group p by p.SomeId into pg
you no longer have access to the range variables used in the initial from
. That is, you can no longer talk about p
or bp
, you can only talk about pg
.
Now, pg
is a group and so contains more than one product. All the products in a given pg
group have the same SomeId
(since that's what you grouped by), but I don't know if that means they all have the same BaseProductId
.
To get a base product name, you have to pick a particular product in the pg
group (As you are doing with SomeId
and CountryCode
), and then join to BaseProducts
.
var result = from p in Products
group p by p.SomeId into pg
// join *after* group
join bp in BaseProducts on pg.FirstOrDefault().BaseProductId equals bp.Id
select new ProductPriceMinMax {
SomeId = pg.FirstOrDefault().SomeId,
CountryCode = pg.FirstOrDefault().CountryCode,
MinPrice = pg.Min(m => m.Price),
MaxPrice = pg.Max(m => m.Price),
BaseProductName = bp.Name // now there is a 'bp' in scope
};
That said, this looks pretty unusual and I think you should step back and consider what you are actually trying to retrieve.
这篇关于LINQ:结合加入和组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!