2014-02-14 116 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; 
    } 
}