LINQ(动态):使用动态 linq 在 GroupBy 中进行 OrderBy?

发布于 2024-12-06 21:49:32 字数 437 浏览 0 评论 0 原文

我使用普通 linq 进行了以下查询,并且效果很好(使用匿名类型),

     var result = from s in Items
            group s by s.StartTime into groupedItems
            select new {groupedItems.Key, Items= groupedItems.OrderBy(x => x.Name) };

但是使用动态 Linq 我无法让它在 groupby 内排序。

     result = Items.GroupBy("StartTime", "it").OrderBy("Name");

它指出名称不可用。值得注意的是,如果我关闭 OrderBy,一切都会很好,但每个“Key”内的项目不会被排序。

I had the following query using normal linq and it was working great (using anonymous type),

     var result = from s in Items
            group s by s.StartTime into groupedItems
            select new {groupedItems.Key, Items= groupedItems.OrderBy(x => x.Name) };

But using Dynamic Linq I cannot get it to order by within the groupby.

     result = Items.GroupBy("StartTime", "it").OrderBy("Name");

It states the Name isn't available. It is worth noting that if I take my OrderBy off, everything works great but items inside each "Key" are not ordered.

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

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

发布评论

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

评论(1

淡墨 2024-12-13 21:49:32

这是一个好问题!

我通过创建一个名为 Item 的类来模拟您的情况。

public class Item
{
    public DateTime StartTime { get; set; }
    public string Name { get; set; }
}

然后创建一个基本的项目列表来进行分组。

List<Item> Items = new List<Item>()
{
    new Item() { StartTime = DateTime.Today, Name = "item2"},
    new Item() { StartTime = DateTime.Today, Name = "item1"},
    new Item() { StartTime = DateTime.Today.AddDays(-1), Name = "item3"},
};

现在,这两个查询的最大区别在于执行 order by 的位置。在第一个查询中,当您执行 groupedItems.OrderBy(x => x.Name) 时,它会在 IGrouping 或单个条目上执行,如下所示它遍历所有分组。

在第二个查询中,orderby 是在事后执行的。这意味着您正在 IEnumerable> 上执行 orderby,因为迭代已经发生。

由于微软很好,他们添加了一些东西来帮助处理表达式的这个问题。 此重载允许您指定在迭代集合时返回的项目。下面是代码示例:

var expressionResult = Items.GroupBy(x => x.StartTime, 
(key, grpItems) => new { key, Items = grpItems.OrderBy(y => y.Name) });

GroupBy 的第二部分您可以指定一个 lambda 表达式,该表达式采用一个键和该键下的项目分组,并返回您指定的条目,这与您在原始查询。

希望这有帮助!

This is a good question!

I simulated your situation by creating a class called Item.

public class Item
{
    public DateTime StartTime { get; set; }
    public string Name { get; set; }
}

and then created a basic list of items to do the groupby.

List<Item> Items = new List<Item>()
{
    new Item() { StartTime = DateTime.Today, Name = "item2"},
    new Item() { StartTime = DateTime.Today, Name = "item1"},
    new Item() { StartTime = DateTime.Today.AddDays(-1), Name = "item3"},
};

Now the big difference in the 2 queries is where the order by is being performed. In the first query, when you perform groupedItems.OrderBy(x => x.Name) its being performed on a IGrouping<DateTime,Item> or a single entry as it iterates through all the groupings.

In the second query, the orderby is being performed after the fact. This means you're doing an orderby on a IEnumerable<IGrouping<DateTime,Item>> because the iterations have already happened.

Since Microsoft was nice they added something to help deal with this for expressions. This overload allows you to specify the item returned as it iterates through the collection. Here's an example of the code:

var expressionResult = Items.GroupBy(x => x.StartTime, 
(key, grpItems) => new { key, Items = grpItems.OrderBy(y => y.Name) });

The second part of the GroupBy you can specify a lambda expression that takes a key and a grouping of items under that key and return an entry that you specify, which is the same as you're doing in the original query.

Hope this helps!

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