Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq-如何在列表的最大元素上应用where子句 我有个人的名单 每个人都有一个类别列表 Categoty有两个属性:日期和值_C#_Linq_Linq To Nhibernate - Fatal编程技术网

C# Linq-如何在列表的最大元素上应用where子句 我有个人的名单 每个人都有一个类别列表 Categoty有两个属性:日期和值

C# Linq-如何在列表的最大元素上应用where子句 我有个人的名单 每个人都有一个类别列表 Categoty有两个属性:日期和值,c#,linq,linq-to-nhibernate,C#,Linq,Linq To Nhibernate,我想选择一个Person列表,其中最后一个类别等于B。但是我不知道如何用Linq语法编写where子句 我的个人结构是: <person> <id>200</id> <name>Peter</name> <age>25</age> <categories> <category> <date>2012-05-

我想选择一个Person列表,其中最后一个类别等于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>

您可以使用以下选项:

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>
-------------------------------------------------

这是linq到xml还是linq到nhibernate?Hibernate。XML只是显示Person实体的字段如何设置日期字段。LastDate和Value可以互换。您会注意到Last返回person.Categories列表中的最后一个类别。因此,您可以使用该对象的任何属性。对不起,最后一个类别是具有最新日期的类别。有时这可能不是最后一次插入,在这种情况下,您必须在where条款中的日期之前订购。请参阅我的编辑。