C# LINQ orderby 在对象数组的单个槽上

发布于 2024-10-19 03:43:36 字数 1563 浏览 1 评论 0原文

我有一个返回 List 的函数。我想按对象中的第一个槽进行分组并按第二个槽进行排序。我已经让组部分正常工作,但似乎无法正确排序语法。下面的代码正确地对结果进行了分组。我希望每个单独的组按日期排序(oject[] 的第二个槽)。有谁知道这个的语法吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;    
namespace Linq_Object_Array_Test
{
    class Program
    {
        static void Main(string[] args)
        {            
            List<object[]> data = new List<object[]>();    
            data.Add(new object[3] { "6752 JT", new DateTime(2011, 1, 12), 3690 });
            data.Add(new object[3] { "6752 JT", new DateTime(2011, 1, 13), 3600 });
            data.Add(new object[3] { "7630 JT", new DateTime(2011, 1, 11), 789 });
            data.Add(new object[3] { "6752 JT", new DateTime(2011, 1, 11), 3694 });
            data.Add(new object[3] { "7630 JT", new DateTime(2011, 1, 12), 780 });
            data.Add(new object[3] { "7630 JT", new DateTime(2011, 1, 13), 769 });
            data.Add(new object[3] { "8719 JT", new DateTime(2011, 1, 11), 43200 });

            // correctly groups by symbol of security
            var query = data.GroupBy(o => o[0]);

            foreach (var item in query)
            {
                Console.Write(item.Key.ToString() + "\n");    
                foreach (var item2 in item)                    
                    Console.Write("  {0}  {1}  {2} \n", item2[0].ToString(), item2[1].ToString(), item2[2].ToString());      
                Console.Write("\n");
            }
        }
    }
}

任何帮助将不胜感激。

I have a function that return a List<object[]>. I would like to group by the first slot in the object and sort by the 2nd. I have gotten the group part to work but can't seem to get sort syntax correct. The code below correctly groups the result. I would like each individual group to be sorted by date (2nd slot of oject[]). Does anyone know the syntax for this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;    
namespace Linq_Object_Array_Test
{
    class Program
    {
        static void Main(string[] args)
        {            
            List<object[]> data = new List<object[]>();    
            data.Add(new object[3] { "6752 JT", new DateTime(2011, 1, 12), 3690 });
            data.Add(new object[3] { "6752 JT", new DateTime(2011, 1, 13), 3600 });
            data.Add(new object[3] { "7630 JT", new DateTime(2011, 1, 11), 789 });
            data.Add(new object[3] { "6752 JT", new DateTime(2011, 1, 11), 3694 });
            data.Add(new object[3] { "7630 JT", new DateTime(2011, 1, 12), 780 });
            data.Add(new object[3] { "7630 JT", new DateTime(2011, 1, 13), 769 });
            data.Add(new object[3] { "8719 JT", new DateTime(2011, 1, 11), 43200 });

            // correctly groups by symbol of security
            var query = data.GroupBy(o => o[0]);

            foreach (var item in query)
            {
                Console.Write(item.Key.ToString() + "\n");    
                foreach (var item2 in item)                    
                    Console.Write("  {0}  {1}  {2} \n", item2[0].ToString(), item2[1].ToString(), item2[2].ToString());      
                Console.Write("\n");
            }
        }
    }
}

Any help would be most appreciated.

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

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

发布评论

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

评论(2

放我走吧 2024-10-26 03:43:36

Enumerable.OrderBy 扩展是您所寻求的:

var query = data.OrderBy(o => o[1]).GroupBy(o => o[0]);

The Enumerable.OrderBy extension is what you seek:

var query = data.OrderBy(o => o[1]).GroupBy(o => o[0]);
帝王念 2024-10-26 03:43:36

我真的会考虑使用匿名类型(或创建一个单独的类)而不是使用 List。这将为您提供更具可读性的代码和类型安全性。

像这样:

var data = new[]{
new { Id = "6752 JT", Date = new DateTime(2011, 1, 12), Num = 3690 },
new { Id = "6752 JT", Date = new DateTime(2011, 1, 13), Num = 3600 },
...,
};

var query = data.OrderBy(o => o.Date).GroupBy(o => o.Id);

I would really consider using an anonymous type (or create a separate class) instead of using List<Object>. That will give you more readable code and type safety.

Like this:

var data = new[]{
new { Id = "6752 JT", Date = new DateTime(2011, 1, 12), Num = 3690 },
new { Id = "6752 JT", Date = new DateTime(2011, 1, 13), Num = 3600 },
...,
};

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