当x. kind = "value"时选择Top(x)

本文关键字:quot Top 选择 value kind | 更新日期: 2023-09-27 17:53:29

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

我需要Linq语句来查看序列中是否有一个中断,并且只返回前两个项目。问题是我不知道到底有多少项具有正确的属性值。

我一直在用LinqPad 4解决这个问题。下面的代码是从LinqPad 4复制过来的。结果,"q",不应该包含SomeData的有效性日期为4/5/2011,因为在hsc2上的Kind属性是"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;}
}

当x. kind = "value"时选择Top(x)

你正在寻找TakeWhile方法

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

这是一个完全工作的控制台应用程序,可以完成您的要求。由于我不是第一个提出在这个问题中使用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);
            }
        }
    }
}

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

如果你只想获取一个子集,你可以使用。take (num)扩展方法。