Linq Lambda GroupBy 和 OrderBy

发布于 2024-10-09 01:28:46 字数 1246 浏览 3 评论 0原文

我想分组,然后对组内的项目进行排序。

我如何用 lamda 做到这一点,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var data = new[]
                  {
                      new { Name="Tasty", Type="Strawberries", isAvail=true, Price=1.90m, Quantity=20 },

                    new { Name="Granny Smith", Type="Apple", isAvail=false, Price=0.80m, Quantity=7 },
                    new { Name="Gala", Type="Apple", isAvail=true, Price=0.75m, Quantity=10 }

                  };

            var grouped = data.GroupBy(record => record.Type).OrderBy(x => x.Min(y => (Decimal)y.Price));

            foreach (var group in grouped)
            {
                Console.WriteLine("Key {0}", group.Key);

                foreach (var item in group)
                {
                    Console.WriteLine("\t{0}", item.Name);
                }
            }
            Console.ReadLine();
        }
    }
}

上面给了我这个..

Key - 苹果

----Granny Smith

----Gala

Key - 草莓

----Tasty

但正如你所看到的,Gala 的价格比 Granny 低史密斯……我做错了什么?请帮忙!

I would like to Group by and then order the items within the group.

how do i do it with lamda,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var data = new[]
                  {
                      new { Name="Tasty", Type="Strawberries", isAvail=true, Price=1.90m, Quantity=20 },

                    new { Name="Granny Smith", Type="Apple", isAvail=false, Price=0.80m, Quantity=7 },
                    new { Name="Gala", Type="Apple", isAvail=true, Price=0.75m, Quantity=10 }

                  };

            var grouped = data.GroupBy(record => record.Type).OrderBy(x => x.Min(y => (Decimal)y.Price));

            foreach (var group in grouped)
            {
                Console.WriteLine("Key {0}", group.Key);

                foreach (var item in group)
                {
                    Console.WriteLine("\t{0}", item.Name);
                }
            }
            Console.ReadLine();
        }
    }
}

the Above gives me this..

Key - Apple

----Granny Smith

----Gala

Key - Strawberries

----Tasty

But as you can see the price of Gala is lower than Granny smith... what am i doing wrong? Plese help!

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

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

发布评论

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

评论(3

孤城病女 2024-10-16 01:28:46

您在订购之前先进行分组。换句话说,您对组进行排序,而不是对组内的项目进行排序。

先尝试订购。

var grouped = data.OrderBy(x => x.Price).GroupBy(record => record.Type); 

这应该有效。

You are Grouping before you are Ordering. In other words, you are ordering the groups rather than the items within the groups.

Trying ordering first.

var grouped = data.OrderBy(x => x.Price).GroupBy(record => record.Type); 

This should work.

桜花祭 2024-10-16 01:28:46

你尝试过吗

var grouped = data.OrderBy(x => x.Price).GroupBy(record => record.Type);

Have you tried

var grouped = data.OrderBy(x => x.Price).GroupBy(record => record.Type);
无法回应 2024-10-16 01:28:46
var grouped = data.GroupBy(record => record.Type)
.Select(g=>new {g.Key,records=g.OrderBy(r=>r.Price)})  

// 此时记录按价格排序(在分组内)

.OrderBy(x => x.records.Min(y => (Decimal)y.Price))

// 现在分组也按最低价格排序

var grouped = data.GroupBy(record => record.Type)
.Select(g=>new {g.Key,records=g.OrderBy(r=>r.Price)})  

// at this point records are ordered by price (inside groupings)

.OrderBy(x => x.records.Min(y => (Decimal)y.Price))

// now groupings are ordered by min price as well

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