2011-04-28 59 views
1

只要列表頂部的項目具有特定值的屬性,我如何選擇列表的頂部。選擇Top(x),而x.Kind =「value」

我需要Linq語句來查看序列中有一箇中斷,並且只返回前兩個項目。問題是我不知道有多少物品會有正確的屬性值。

我一直在通過LinqPad 4解決這個問題。下面的代碼是LinqPad 4的副本和過去的代碼。結果「q」不應該包含SomeData,因爲有效日期爲4/5/2011,因爲Kind hsc2的財產是「KindTwo」。

我正在嘗試查找「Kind」的最重新值的值,然後只取與該值匹配的最高記錄,直到找到與該值不匹配的記錄。

void Main() 
{ 
    var hsc1 = new SomeData {EffectiveDate = new DateTime(2011,4,5), Kind = "KindOne"}; 
    var hsc2 = new SomeData {EffectiveDate = new DateTime(2011,4,10), Kind = "KindTwo"}; 
    var hsc3 = new SomeData {EffectiveDate = new DateTime(2011,4,20), Kind = "KindOne"}; 
    var hsc4 = new SomeData {EffectiveDate = new DateTime(2011,4,25), Kind = "KindOne"}; 

    var all = new [] {hsc1, hsc2, hsc3, hsc4}; 

    var lastSomeData = all.OrderByDescending((x) => x.EffectiveDate).First(); 

    lastSomeData.Dump(); 

    var q = from h in all 
      where h.Kind == lastSomeData.Kind 
      orderby h.EffectiveDate descending 
      select h; 

    q.Dump(); 
} 

// Define other methods and classes here 
class SomeData 
{ 
    public DateTime EffectiveDate {get;set;} 
    public string Kind {get;set;} 
} 

回答

1

這是一個完全工作的控制檯應用程序已經做了你問什麼。由於我不是第一個提出在這個問題中使用TakeWhile的人,請不要將我的答案標記爲已接受的答案。

using System; 
using System.Linq; 
namespace stackoverflow.com_questions_5825629_select_topx_while_x_kind_value 
{ 
    class Program 
    { 
     static void Main() 
     { 
      var hsc1 = new SomeData { EffectiveDate = new DateTime(2011, 4, 5), Kind = "KindOne" }; 
      var hsc2 = new SomeData { EffectiveDate = new DateTime(2011, 4, 10), Kind = "KindTwo" }; 
      var hsc3 = new SomeData { EffectiveDate = new DateTime(2011, 4, 20), Kind = "KindOne" }; 
      var hsc4 = new SomeData { EffectiveDate = new DateTime(2011, 4, 25), Kind = "KindOne" }; 

      var all = new[] { hsc1, hsc2, hsc3, hsc4 }; 

      var lastSomeData = all.OrderByDescending((x) => x.EffectiveDate).First(); 

      var q = (from h in all 
        orderby h.EffectiveDate descending 
        select h).TakeWhile(x => x.Kind == lastSomeData.Kind); 

      var result = q.ToArray(); 

      foreach (var item in result) 
       Console.WriteLine(item); 
      Console.WriteLine(""); 
      Console.WriteLine("Press any key"); 
      Console.ReadKey(); 
     } 

     // Define other methods and classes here 
     class SomeData 
     { 
      public DateTime EffectiveDate { get; set; } 
      public string Kind { get; set; } 
      public override string ToString() 
      { 
       return string.Format(@"new SomeData {{ EffectiveDate = new DateTime({0}, {1}, {2}), Kind = ""{3}"" }};", EffectiveDate.Year, EffectiveDate.Month, EffectiveDate.Day, Kind); 
      } 
     } 
    } 
} 
+0

您不是第一個推薦TakeWhile解決方案的人,但是您是第一個從查詢中刪除where子句的人,並且這是我的阻止問題。 – 2011-04-29 18:43:31

0

爲什麼不應該這樣呢?按日期降序排序可以獲得2011/4/25的第一個元素和一種KindOne。由於2011/4/5日期的元素有一種KindOne,它將包含在結果中。

如果您只想抓取子集,可以使用.Take(num)擴展方法。

4

您正在尋找的TakeWhile方法

DateTime filterDate = new DateTime(2011, 4, 5); 
var top = q.TakeWhile(x => (DateTime.Compare(x, filterDate) != 0)); 
+0

該過濾器的種類不是生效日期。如果我使用TakeWhile,那麼我仍然會返回Data對象,其生效日期爲2011年4月5日。我需要過濾掉最後兩個,因爲Kind屬性已更改。我只想要前兩個,因爲Kind值匹配。 – 2011-04-29 02:22:35