1

我对 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_nameSurnameSalesmans

4

3 回答 3

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),
    };
于 2013-11-04T16:50:38.100 回答
0

将当前行更改为此。

select new { Salesman_ID = g.Key, Salesman_First_Name = g.Key.First_name };
于 2013-11-04T16:51:35.023 回答
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),
                    };
于 2013-11-04T16:52:55.253 回答