在 C# 中对列表内的列表进行排序

发布于 2024-11-28 01:47:33 字数 267 浏览 1 评论 0原文

我需要在使用 Linq 排序数据方面朝着正确的方向推动。

我有一个 C# 中的 People 对象列表,该列表又包含该人员可能与之关联的站点列表。

这些站点对象保存的是组织对象的实例,该对象与站点关联(因为一个组织可能有许多站点..)

编辑:一个人仅属于列表中的单个组织。 (感谢@jonskeet)

我如何对人员列表进行排序,使用 Linq(或 Lamba)按组织的字母顺序显示他们,然后按联系人的姓氏、名字排序..?

I need a push in the right direction with regards to ordering data using Linq.

I've got a list of People objects in C#, which in turn, holds a list of Sites that the person may be associated with.

Those Site objects holds is an instance of an Organisation object, that the site is associated with (as an organisation may have many sites..)

Edit: A person will only belong to a single organisation in the list. (Thanks @jonskeet)

How do I order the list of people, using Linq (or Lamba) to show them in alphabetical order, by their Organisation, and then ordered by the Surname, Firstname of the contact..??

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

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

发布评论

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

评论(3

鲜肉鲜肉永远不皱 2024-12-05 01:47:33

我觉得你的模型有点混乱,但如果你确定:

  • 一个人至少有一个网站
  • 所有特定人的网站都将属于同一个组织,

你可以使用:

var sorted = people.OrderBy(p => p.Sites.First().Organization)
                   .ThenBy(p => p.LastName)
                   .ThenBy(p => p.FirstName);

It strikes me that your model is a bit messed up, but if you're sure that:

  • A person will have at least one site
  • All the sites for a particular person will be for the same organization

you can use:

var sorted = people.OrderBy(p => p.Sites.First().Organization)
                   .ThenBy(p => p.LastName)
                   .ThenBy(p => p.FirstName);
红ご颜醉 2024-12-05 01:47:33
people.SelectMany(p => p.Sites).OrderBy(s => s.Organisation.Name).ThenBy(s => s.Organisation.Contact.Surname).ThenBy(s => s.Organisation.Contact.FirstName);
people.SelectMany(p => p.Sites).OrderBy(s => s.Organisation.Name).ThenBy(s => s.Organisation.Contact.Surname).ThenBy(s => s.Organisation.Contact.FirstName);
清晰传感 2024-12-05 01:47:33

Linq 支持 order by 子句。查看此 MSDN 链接

http://msdn.microsoft.com/en-us/库/bb383982.aspx

示例

public class CityLookup
{       public string City { get; set; }    
    public string Country { get; set; }
}

List<CityLookup> citiesLkp =            
new List<CityLookup>            
{       
    new CityLookup{ City = "New Delhi", Country = "India" },          
    new CityLookup{ City = "Sydney", Country = "Australia" },  
    new CityLookup{ City = "Tokyo", Country = "Japan" },                 
    new CityLookup{ City = "New York", Country = "USA" },                
    new CityLookup{ City = "Paris", Country = "France" },                
    new CityLookup{ City = "Barcelona", Country = "Spain" },                                          
};


// Now sort the names based on city name
var myList =    from c in citiesLkp    orderby c.City    select c;

foreach (var record in myList )    
{        
    Console.WriteLine(record.City);    
}

Linq supports an order by clause. Check out this MSDN link

http://msdn.microsoft.com/en-us/library/bb383982.aspx

Example

public class CityLookup
{       public string City { get; set; }    
    public string Country { get; set; }
}

List<CityLookup> citiesLkp =            
new List<CityLookup>            
{       
    new CityLookup{ City = "New Delhi", Country = "India" },          
    new CityLookup{ City = "Sydney", Country = "Australia" },  
    new CityLookup{ City = "Tokyo", Country = "Japan" },                 
    new CityLookup{ City = "New York", Country = "USA" },                
    new CityLookup{ City = "Paris", Country = "France" },                
    new CityLookup{ City = "Barcelona", Country = "Spain" },                                          
};


// Now sort the names based on city name
var myList =    from c in citiesLkp    orderby c.City    select c;

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