3
  • 私は「人」のリストを持っています
  • 各人物には「カテゴリ」のリストがあります
  • カテゴリには、日付と値の 2 つの属性があります。

最後のカテゴリが「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>
4

3 に答える 3

5

以下を使用できます。

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();
于 2013-03-01T19:41:22.700 に答える
3

カテゴリが日付によって順不同になる可能性があると仮定すると、次のようになります。

var bPersons = persons.Where(p => 
                             p.Categories
                              .OrderByDescending(c => c.Date)
                              .First().Value == "B")
于 2013-03-01T19:42:36.583 に答える
0

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>
-------------------------------------------------
于 2013-03-01T20:29:11.053 に答える