2014-02-14 76 views
0

我有一个自定义列表,如下所示。C#Linq Groupby和Where

class ActionToDo 
{ 
    public string Name {get;set;} 
    public DateTime dtDate {get;set;} 
    public string EventCode {get;set;} 
    public string EventDescription {get;set;} 
} 

我想这样做的是找到具有相同日期&同一事件的描述以及其中EventCode = "AQ"项目。我猜LINQ是实现这个目标的最好方法吗?我不知道如何使用LINQ来做到这一点。

在我的脑海中使用Sql我认为它大致如下图所示。

SELECT * FROM SomeTable 
WHERE [EventDescription] = 'AQ' 
GROUP BY [dtDate], [EventDescription]  

回答

2
.Where(x=> x.EventDescription == "AQ") 
.GroupBy(x => new { x.dtDate.Date, x.EventDescription}) 
2

因此,让我们假设你有一个

List<ActionToDo> actionToDo = new List<ActionToDo>(); 
actionToDo.Where(i => i.EventDescription == "AQ" && i.EventCode="AQ") 
.GroupBy(i => new { i.dtDate.Date, i.EventDescription }); 
1

下面是一个LINQ查询使用,以满足您的要求查询语法表示。此外,它也由Name排序分组属性dtDateEventDescription,然后将每个组的成员:

var actions = new List<ActionToDo>(); 
// populate 'actions' 

var results = 
    from a in actions 
    where a.EventCode == "AQ" 
    orderby a.dtDate, a.EventDescription, a.Name 
    group a by new { a.dtDate, a.EventDescription }; 

为了证明这个查询,我创建了一个程序与随机顺序一些样品ActionToDo数据。请看下面的程序格式化输出,然后是程序本身。

演示程序输出

[2014-02-12] [Desc.AQ.12] 
    AQ.12a 
    AQ.12b 
[2014-02-13] [Desc.AQ.13] 
    AQ.13a 
    AQ.13b 
    AQ.13c 
[2014-02-14] [Desc.AQ.14] 
    AQ.14a 
    AQ.14b 

示范项目

using System; 
using System.Collections.Generic; 
using System.Linq; 

class GroupByDemo 
{ 
    static public void Main(string[] args) 
    { 
     var actions = new List<ActionToDo>() 
      { 
       new ActionToDo("AQ.14b", "2014-02-14", "AQ", "Desc.AQ.14"), 
       new ActionToDo("AQ.12a", "2014-02-12", "AQ", "Desc.AQ.12"), 
       new ActionToDo("AQ.13b", "2014-02-13", "AQ", "Desc.AQ.13"), 
       new ActionToDo("XX.01", "2014-02-01", "XX", "Desc.XX.01"), 
       new ActionToDo("AQ.14a", "2014-02-14", "AQ", "Desc.AQ.14"), 
       new ActionToDo("AQ.12b", "2014-02-12", "AQ", "Desc.AQ.12"), 
       new ActionToDo("AQ.13a", "2014-02-13", "AQ", "Desc.AQ.13"), 
       new ActionToDo("XX.02", "2014-02-02", "XX", "Desc.XX.02"), 
       new ActionToDo("AQ.13c", "2014-02-13", "AQ", "Desc.AQ.13"), 
       new ActionToDo("XX.03", "2014-02-03", "XX", "Desc.XX.03") 
      }; 

     var results = 
      from a in actions 
      where a.EventCode == "AQ" 
      orderby a.dtDate, a.EventDescription, a.Name 
      group a by new { a.dtDate, a.EventDescription }; 

     foreach (var group in results) 
     { 
      Console.WriteLine("[{0}] [{1}]", 
           group.Key.dtDate.ToString("yyyy-MM-dd"), 
           group.Key.EventDescription); 

      foreach (var action in group) 
      { 
       Console.WriteLine(" {0}", action.Name); 
      } 
     } 
    } 
} 

class ActionToDo 
{ 
    public string Name {get;set;} 
    public DateTime dtDate {get;set;} 
    public string EventCode {get;set;} 
    public string EventDescription {get;set;} 

    public ActionToDo(
     string name, 
     string dtDateString, 
     string eventCode, 
     string eventDescription) 
    { 
     this.Name = name; 
     this.dtDate = DateTime.Parse(dtDateString); 
     this.EventCode = eventCode; 
     this.EventDescription = eventDescription; 
    } 
}