我还有LINQ的另一个问题,所以我要做的是创建一个最好的推销员列表。我的sold_items
表格看起来像这样
[ID]
[Name]
[Price]
[Sold_date]
[Salesman_ID]
和Salesmans
[ID]
[Login]
[First_name]
[Surname]
我想总结每个销售人员出售的所有价格,并且还可以选择按日期分组,例如本月最佳或本周
var query1 = from s in context.Archive
//join us in context.Users
//on s.Salesman.ID equals us.ID
group s by s.Salesman.ID
into g
orderby g.Sum(o => o.Price) descending
select new
{
Salesman_ID = g.Key,
Sale = g.Sum(o => o.Price),
};
效果很好,但我只获得salesman_ID
,如何从First_name
表中添加用户Surname
和Salesmans
?
答案 0 :(得分:2)
你可以这样做:
var query1 =
from s in context.Archive
group s by s.Salesman into g
orderby g.Sum(o => o.Price) descending
select new
{
Salesman_ID = g.Key.ID,
Salesman_First_name = g.Key.First_name,
Salesman_Surname = g.Key.Surname,
Sale = g.Sum(o => o.Price),
};
要限制我的具体日期,请使用以下内容:
var query1 =
from s in context.Archive
where s.Sold_date >= minDate && s.Sold_date <= maxDate
group s by s.Salesman into g
orderby g.Sum(o => o.Price) descending
select new
{
Salesman_ID = g.Key.ID,
Salesman_First_name = g.Key.First_name,
Salesman_Surname = g.Key.Surname,
Sale = g.Sum(o => o.Price),
};
答案 1 :(得分:0)
将当前行更改为此。
select new { Salesman_ID = g.Key, Salesman_First_Name = g.Key.First_name };
答案 2 :(得分:0)
您可以按多列(要检索的所有列)进行分组
var query1 = from s in context.Archive
//join us in context.Users
//on s.Salesman.ID equals us.ID
group s by new // group by multiple columns
{
s.Salesman.Id,
s.first_name,
s.surname,
} into g
orderby g.Sum(o => o.Price) descending
select new
{
Salesman_ID = g.Key,
FirstName = g.first_name,
Surname = g.surname,
Sale = g.Sum(o => o.Price),
};