使用 Linq 对 Person 对象列表进行排序

发布于 2024-10-24 17:12:34 字数 1221 浏览 2 评论 0原文

我需要根据对象属性的类别对对象列表进行查询。我需要这些组的顺序不同于我在许多其他示例中看到的通常的字母顺序。我正在使用从其他地方获取的示例。如何根据 HomeProvince 但按照以下顺序生成 Person 对象列表:

安大略省、魁北克省、艾伯塔省、马尼托巴省、不列颠哥伦比亚省。每个组内的顺序并不重要。

Person[] people = new Person[]
{
    new Person() { FirstName = "Tony", LastName = "Montana", Age = 39, HomeProvince = "Ontario" },
    new Person() { FirstName = "Bill", LastName = "Smith", Age = 23, HomeProvince = "Ontario" },
    new Person() { FirstName = "Jane", LastName = "Doe", Age = 23, HomeProvince = "Alberta" },
    new Person() { FirstName = "John", LastName = "Doe", Age = 23, HomeProvince = "Alberta" },
    new Person() { FirstName = "Alex", LastName = "DeLarge", Age = 19, HomeProvince = "British Columbia" },
    new Person() { FirstName = "Travis", LastName = "Bickle", Age = 42, HomeProvince = "Quebec" },
    new Person() { FirstName = "Ferris", LastName = "Beuller", Age = 17, HomeProvince = "Manitoba" },
    new Person() { FirstName = "Maggie", LastName = "May", Age = 23, HomeProvince = "Ontario" },
    new Person() { FirstName = "Mickey", LastName = "Mouse", Age = 93, HomeProvince = "Alberta" },
    new Person() { FirstName = "Frank", LastName = "Darabont", Age = 49, HomeProvince = "Ontario" }
};

I need a query on sorting list of objects based on the category of property of the objects. I need the groups to be in an order other than the usual alphabetical order which I have seen in many other samples. I am using an example I took from elsewhere. How can I generate a list of Person objects based on HomeProvince but in terms of this ordering:

Ontario, Quebec, Alberta, Manitoba, British Columbia. The ordering within each group does not matter.

Person[] people = new Person[]
{
    new Person() { FirstName = "Tony", LastName = "Montana", Age = 39, HomeProvince = "Ontario" },
    new Person() { FirstName = "Bill", LastName = "Smith", Age = 23, HomeProvince = "Ontario" },
    new Person() { FirstName = "Jane", LastName = "Doe", Age = 23, HomeProvince = "Alberta" },
    new Person() { FirstName = "John", LastName = "Doe", Age = 23, HomeProvince = "Alberta" },
    new Person() { FirstName = "Alex", LastName = "DeLarge", Age = 19, HomeProvince = "British Columbia" },
    new Person() { FirstName = "Travis", LastName = "Bickle", Age = 42, HomeProvince = "Quebec" },
    new Person() { FirstName = "Ferris", LastName = "Beuller", Age = 17, HomeProvince = "Manitoba" },
    new Person() { FirstName = "Maggie", LastName = "May", Age = 23, HomeProvince = "Ontario" },
    new Person() { FirstName = "Mickey", LastName = "Mouse", Age = 93, HomeProvince = "Alberta" },
    new Person() { FirstName = "Frank", LastName = "Darabont", Age = 49, HomeProvince = "Ontario" }
};

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

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

发布评论

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

评论(2

娜些时光,永不杰束 2024-10-31 17:12:34

您可以这样做:

// Provinces in the desired order
string[] provinces = { "Ontario", "Quebec", "Alberta", "Manitoba", 
                       "British Columbia" };

var query = from province in provinces
            join person in people on province equals person.HomeProvince
            select person;

这基本上会:

  • 忽略不在指定省份的任何人
  • 按指定的省份顺序返回人员序列

如果您需要按省份分组的人员,那也很简单:

var query = from province in provinces
            join person in people on province equals person.HomeProvince
                into grouped
            select new { Province = province, People = grouped.ToList() };

另一种选择是创建从省份到“优先级”的映射并简单地按该顺序排序。这实际上取决于您需要什么作为输出。

You could do this:

// Provinces in the desired order
string[] provinces = { "Ontario", "Quebec", "Alberta", "Manitoba", 
                       "British Columbia" };

var query = from province in provinces
            join person in people on province equals person.HomeProvince
            select person;

That will basically:

  • Ignore anyone not in the specified provinces
  • Return a sequence of people in the province order specified

If you need the people grouped by province, that's easy too:

var query = from province in provinces
            join person in people on province equals person.HomeProvince
                into grouped
            select new { Province = province, People = grouped.ToList() };

Another option would be to create a mapping from province to "priority" and simply order by that. It really depends on exactly what you need as output.

呆° 2024-10-31 17:12:34

我不确定这是否是最好的方法,但我可能会让每个以安大略省为家乡的人并将他们添加到数组中。然后重复魁北克等......我不知道任何其他方式,因为你需要的是“随机”。

我说数组是因为这就是您在代码示例中使用的数组。显然,只要您不在放入物品时或放入之后订购它们,那么使用哪种容器并不重要。

I'm not sure if this is the best way but I would probably just get every person with Ontario as HomeProvince and add them to the array. Then repeat for Quebec and so on... I don't know any other way because what you need is "random".

I say array because that's what you used in your code sample. Obviously it doesn't matter what kind of container you use as long as you don't order them as you put them in or after they are in.

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