我再次挣扎,所以任何帮助都会感激不尽。
我有以下LINQ可以回读一个数据列表:
public static List<tblWeight> GetWeights(string memberid, string locationid, string buyer, string subcategory, string product)
{
MyEntity getweights = new MyEntity ();
var r = (from p in getweights.tblWeights
where p.MemberId == memberid &&
p.LocationId == locationid
select p);
if (buyer != "Not Specified")
r = r.Where(p => p.UnitUserField1 == buyer);
if (subcategory != "Not Specified")
r = r.Where(p => p.UnitUserField2 == subcategory);
if (product != "Not Specified")
r = r.Where(p => p.IDDesc == product);
return r.ToList();
}
可爱!
我现在要做的是根据这个结果集和单位ID(IDDesc),然后转到tblPurchase,从tblPurchases中拉回几列并对列进行分组。
例如,我们看起来像tblWeight:
MemberID LocationID Buyer SubCategory IDDesc
1 1 Cat1 Sub1 ab
1 1 Cat1 Sub1 abc
1 1 Cat1 Sub2 abcd
用户在子类别中搜索Sub1,上面的LINQ完成了这一操作并从上面拉回前两行。细
我现在需要LINQ去做tblPurchases:
MemberID LocationID IDDesc SupplierID SupplierStatus
1 1 ab Sup1 Live
1 1 abc Sup1 Live
1 1 abcd Sup2 Dead
然后撤回以下结果,以便它在MemberID,LocationID和IDDesc上加入,但只选择tblPurchases。
Sup1 Live(或tblPurchases中的所有列,只是分组/不同)
我试图添加一个连接,但无论有多少不同的变化,我仍然遇到了红色的厄运![/ p>
如果有人可以提供帮助,可以再次提供啤酒/吻。
答案 0 :(得分:2)
以下LINQ查询应该执行您想要的操作:
var result = from w in tblWeight
where w.SubCategory == "Sub1"
join p in tblPurchases on
new { w.MemberID, w.LocationID, w.IDDesc } equals
new { p.MemberID, p.LocationID, p.IDDesc }
group p by new { p.SupplierID, p.SupplierStatus } into pg
select pg.Key;
变量result
是一个包含SupplierID
和SupplierStatus
元组的列表。
如果你还想把条件部分放在那里,它会变得有点复杂。这是如何做到的:
var weights = from w in tblWeight
select w;
weights = weights.Where(w => w.SubCategory == "Sub1");
// You can add additional where clauses here.
// Now join with tblPurchases and group by SupplierID and SupplierStatus.
var result =
weights.Join(tblPurchases,
w => new { w.MemberID, w.LocationID, w.IDDesc },
p => new { p.MemberID, p.LocationID, p.IDDesc },
(w, p) => p)
.GroupBy(p => new { p.SupplierID, p.SupplierStatus },
(k, ps) => new
{
k.SupplierID,
k.SupplierStatus,
TotalQty = ps.Sum(p => p.PurchaseQty)
});