2

我有一个包含以下字段的表:

public int S_ID       
public DateTime S_Date            
public string S_Month        
public string S_Overtime    
public string S_WorkingTime

我想(Orderby S_Date), (GroupBy S_Month), (SUM S_Overtime),(SUM S_Workingtime)然后我使用 Binding 在 ListBox 中显示它。

下面的代码有效,但它既不加班也不加班

var manader = (from c in db.nys
               orderby c.S_Date ascending
               select c).GroupBy(g => g.S_Month).Select(x => x.FirstOrDefault());
listBox.ItemsSource = manader;
4

2 回答 2

4

我不知道你在S_WorkingTimeand中有什么数据S_Overtime,但假设这是一个字符串表示形式int

var manader = from c in db.nys
              group c by c.S_Month into g
              select new { 
                    Month = g.Key, 
                    Overtimes = g.Sum(x => Int32.Parse(x.S_Overtime)),
                    WorkingTimes = g.Sum(x => Int32.Parse(x.S_WorkingTime)) 
              };

顺便说一句,我认为按 排序项目是没有意义的S_Date,因为您将它们分组S_Month并使用聚合值。

于 2012-10-15T17:59:23.307 回答
1
db.nys.OrderBy(o => o.S_Date)
    .GroupBy(o => o.S_Month)
    .Select(g =>
        new
        {
            Overtime = g.Sum(v => v.S_Overtime),
            Workingtime = g.Sum(v => v.S_Workingtime)
        }
    )
于 2012-10-15T18:01:25.117 回答