C# LINQ orderby 在对象数组的单个槽上
我有一个返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Enumerable.OrderBy 扩展是您所寻求的:
The Enumerable.OrderBy extension is what you seek:
我真的会考虑使用匿名类型(或创建一个单独的类)而不是使用
List
像这样:
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: