Question

  • I have a list of "Person"
  • Each Person has a list of "Category"
  • Categoty has two atributes: Date and Value

I want to select a list of Person, where the last category equals "B". But I don't know how to write the "where clause" in Linq syntax.

My "person" structure is:

<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>
Was it helpful?

Solution 2

If we are to assume that categories can be out of order by date:

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

OTHER TIPS

You can use the following:

List<Person> allPersons = GetListOfPersons();

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

or the query style

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

If you are just using XML, you can do this:

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

    }
}

This prints:

-------------------------------------------------
<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>
-------------------------------------------------
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top