选择顶部(x),而x.Kind =“value”

时间:2011-04-28 22:10:25

标签: c# linq loops

只要列表顶部的项目具有特定值的属性,如何选择列表顶部。

我需要Linq语句才能看到序列中断,并且只返回前两项。问题是我不确切知道有多少项具有正确的属性值。

我一直在使用LinqPad 4解决这个问题。下面的代码是来自LinqPad 4的副本和过去。结果“q”不应包含有效日期为4/5/2011的SomeData,因为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;}
}

3 个答案:

答案 0 :(得分:4)

您正在寻找TakeWhile方法

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

答案 1 :(得分: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);
            }
        }
    }
}

答案 2 :(得分:0)

为什么不应该呢?按日期降序排序可以获得2011/4/25的第一个元素和一种KindOne。由于具有日期2011/4/5的元素具有一种KindOne,因此它将包含在结果中。

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