Linq-如何在列表的最大元素上应用where子句

本文关键字:元素 应用 where 子句 列表 Linq- | 更新日期: 2023-09-27 18:01:51

  • 我有一个"人物"列表
  • 每个人都有一个"类别"列表
  • 分类法有两个要素:日期和价值

我想选择一个人员列表,其中最后一个类别等于"B"。但我不知道如何用Linq语法编写"where子句"。

我的"人"结构是:

<person>
    <id>200</id>
    <name>Peter</name>
    <age>25</age>
    <categories>
        <category>
            <date>2012-05-01<date>
            <value>A</value>
        </category>
        <category>
            <date>2013-01-01<date>
            <value>B</value>
        </category>
        <category>
            <date>2013-02-01<date>
            <value>C</value>
        </category>
    </categories>
</person>

Linq-如何在列表的最大元素上应用where子句

您可以使用以下内容:

List<Person> allPersons = GetListOfPersons();
List<Person> selectedPersons = allPersons
    .Where((x) => x.Categories
                   .OrderBy(y => y.Date)
                   .Last()
                   .Value == "B")
    .ToList();

或查询式

List<Person> selectedPersons = (from person in allPersons
                                where person.Categories.OrderBy(x => x.Date).Last().Value == "B"
                                select person).ToList();

如果我们假设类别可能会因日期而无序:

var bPersons = persons.Where(p => 
                             p.Categories
                              .OrderByDescending(c => c.Date)
                              .First().Value == "B")

如果您只是使用XML,您可以执行以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace ConsoleApplication2
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var persons = XElement.Parse(xml);
            var seq2 = from person in persons.Descendants("person")
                       where (string) person.Descendants("value").Last() == "B"
                       select person;
            print(seq2);
        }
        private static void print<T>(IEnumerable<T> seq)
        {
            foreach (var item in seq)
            {
                Console.WriteLine("-------------------------------------------------");
                Console.WriteLine(item);
            }
            Console.WriteLine("-------------------------------------------------");
        }
        static string xml =
@"<persons>
<person>
    <id>200</id>
    <name>Peter</name>
    <age>25</age>
    <categories>
        <category>
            <date>2012-05-01</date>
            <value>B</value>
        </category>
        <category>
            <date>2013-01-01</date>
            <value>A</value>
        </category>
        <category>
            <date>2013-02-01</date>
            <value>C</value>
        </category>
    </categories>
</person>
<person>
    <id>201</id>
    <name>Mary</name>
    <age>25</age>
    <categories>
        <category>
            <date>2012-05-01</date>
            <value>A</value>
        </category>
        <category>
            <date>2013-01-01</date>
            <value>B</value>
        </category>
        <category>
            <date>2013-02-01</date>
            <value>C</value>
        </category>
    </categories>
</person>
<person>
    <id>202</id>
    <name>Midge</name>
    <age>25</age>
    <categories>
        <category>
            <date>2012-05-01</date>
            <value>C</value>
        </category>
        <category>
            <date>2013-01-01</date>
            <value>A</value>
        </category>
        <category>
            <date>2013-02-01</date>
            <value>B</value>
        </category>
    </categories>
</person>
</persons>
";
    }
}

此打印:

-------------------------------------------------
<person>
  <id>202</id>
  <name>Midge</name>
  <age>25</age>
  <categories>
    <category>
      <date>2012-05-01</date>
      <value>C</value>
    </category>
    <category>
      <date>2013-01-01</date>
      <value>A</value>
    </category>
    <category>
      <date>2013-02-01</date>
      <value>B</value>
    </category>
  </categories>
</person>
-------------------------------------------------