选择 Top(x),同时 x.Kind = "value"

发布于 2024-11-04 03:51:25 字数 1125 浏览 0 评论 0 原文

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

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

我一直在使用 LinqPad 4 解决这个问题。下面的代码是 LinqPad 4 的复制和过去。结果“q”不应包含有效日期为 4/5/2011 的 SomeData,因为 hsc2 上的 Kind 属性是“二号”。

我试图找到“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;}
}

How do I select the top of the list as long as the items at the top of the list have a property with a specific value.

I need the Linq statement to see that there is a break in the sequence and the only return the top two items. The problem is that I don't know exactly how many items are going to have the correct property value.

I have been working through this problem with LinqPad 4. The code below is a copy and past from LinqPad 4. The results, "q", should not contain SomeData with an EffectiveDate of 4/5/2011 because the Kind property on hsc2 is "KindTwo".

I am trying to find the value most resent value of "Kind" and then only take the top records that match that value until I get to a record that doesn't match that value.

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;}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

折戟 2024-11-11 03:51:25

您正在寻找 TakeWhile 方法

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

You're looking for the TakeWhile method

DateTime filterDate = new DateTime(2011, 4, 5);
var top = q.TakeWhile(x => (DateTime.Compare(x, filterDate) != 0));
清醇 2024-11-11 03:51:25

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

This is a fully working Console Application that does what you asked. As I was not the first to propose the use of TakeWhile in this question, please do not mark my answer as the accepted one.

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);
            }
        }
    }
}
红墙和绿瓦 2024-11-11 03:51:25

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

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

Why shouldn't it? Ordering by date descending gets you a first element of 2011/4/25 and a kind of KindOne. Since the element with date 2011/4/5 has a kind of KindOne, it's going to be included in the result.

If you just want to grab a subset, you can use the .Take(num) extension method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文