改进/修改 LINQ 查询

发布于 2024-12-10 18:37:28 字数 625 浏览 0 评论 0原文

我已经有一个包含一些组的变量。我使用以下 LINQ 查询生成了它:

var historyGroups = from payee in list
                                group payee by payee.Payee.Name into groups
                                orderby groups.Key                                
                                select new {PayeeName = groups.Key, List = groups };

现在我的 HistoryGroups 变量可以包含许多组。每个组都有一个字符串键,结果视图根据该键进行排序。现在,在每个组内都有一个与该键对应的列表。在该 List 内部有一些元素,每个元素都是一种特定类型的对象。它的字段之一是 System.DateTime 类型。我想按日期对这个内部列表进行排序。

谁能帮忙解决这个问题吗?可以修改上面的查询或对变量historyGroups进行新的查询。

谢谢

I already have a variable containing some groups. I generated that using the following LINQ query:

var historyGroups = from payee in list
                                group payee by payee.Payee.Name into groups
                                orderby groups.Key                                
                                select new {PayeeName = groups.Key, List = groups };

Now my historyGroups variable can contain many groups. Each of those groups has a key which is a string and Results View is sorted according to that. Now inside each of those groups there is a List corresponding to the key. Inside that List there are elements and each one those element is an object of a particular type. One of it's fields is of type System.DateTime. I want to sort this internal List by date.

Can anyone help with this? May be modify the above query or a new query on variable historyGroups.

Thanks

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

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

发布评论

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

评论(1

心房的律动 2024-12-17 18:37:28

我不清楚您想要对什么进行排序(也缺少收款人类型定义)

var historyGroups = from payee in list
    group payee by payee.Payee.Name into groups
    orderby groups.Key                                
    select new {
         PayeeName = groups.Key, 
         List = groups.OrderBy(payee2 => payee2.SomeDateTimeField)
    };

是最直接的。

如果您确实只想按日期(而不是时间)排序,请使用 SomeDateTimeField.Date

该列表内有一些元素,每个元素都是一种特定类型的对象。其中一个字段的类型为 System.DateTime

这让我可能(?)怀疑

List = groups.OrderBy(payee2 => payee2.ParticularTypedElement.DateTimeField)

或者甚至

List = groups.OrderBy(payee2 => payee2.ObjectsOfParticularType
      .OfType<DateTime>()
      .FirstOrDefault()
)

我希望下次你能更好地澄清这个问题,所以我们不必猜测那么多(并提出一个令人困惑的答案)

It is not clear to me what you want to sort on (the payee type definition is missing as well)

var historyGroups = from payee in list
    group payee by payee.Payee.Name into groups
    orderby groups.Key                                
    select new {
         PayeeName = groups.Key, 
         List = groups.OrderBy(payee2 => payee2.SomeDateTimeField)
    };

Is most straightforward.

If you really want to sort only by date (and not time), use SomeDateTimeField.Date.

Inside that List there are elements and each one those element is an object of a particular type. One of it's fields is of type System.DateTime

This leads me to maybe(?) suspect

List = groups.OrderBy(payee2 => payee2.ParticularTypedElement.DateTimeField)

Or perhaps even

List = groups.OrderBy(payee2 => payee2.ObjectsOfParticularType
      .OfType<DateTime>()
      .FirstOrDefault()
)

I hope next time you can clarfy the question a bit better, so we don't have to guess that much (and come up with a confusing answer)

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