Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

LINQ: How to select entire list of list by certain object property value

It is probably an easy one but i could not find an answer.
I have List<List<myclass>>, how can i extract entire List by a specific value object property value?

for example, please see my draft code, how can i extract the entire (all the objects in that list) t2 List based on mc7 object properties?

I am only succeeding to extract mc7object but not the List of it in result variable.

class myclass
{
    public DateTime SomeDate;
    public string SomeString;
}

static void Main(string[] args)
{
    List<List<myclass>> L = new List<List<myclass>>();
    List<myclass> t = new List<myclass>();

    myclass mc = new myclass() { SomeString = "5", SomeDate = DateTime.Now};
    myclass mc1 = new myclass() { SomeString = "12", SomeDate = DateTime.Now.AddDays(1) };
    myclass mc2 = new myclass() { SomeString = "123", SomeDate = DateTime.Now.AddDays(2) };
    myclass mc3 = new myclass() { SomeString = "77", SomeDate = DateTime.Now.AddDays(3) };
    myclass mc4 = new myclass() { SomeString = "882", SomeDate = DateTime.Now.AddDays(4) };

    t.Add(mc);
    t.Add(mc1);
    t.Add(mc2);
    t.Add(mc3);
    t.Add(mc4);

    L.Add(t);

    List<myclass> t2 = new List<myclass>();

    myclass mc5 = new myclass() { SomeString = "166", SomeDate = DateTime.Now.AddDays(500) };
    myclass mc6 = new myclass() { SomeString = "344", SomeDate = DateTime.Now.AddDays(501) };
    myclass mc7 = new myclass() { SomeString = "123", SomeDate = DateTime.Now.AddDays(502) };
    myclass mc8 = new myclass() { SomeString = "234", SomeDate = DateTime.Now.AddDays(503) };
    myclass mc9 = new myclass() { SomeString = "123", SomeDate = DateTime.Now.AddDays(504) };

    t2.Add(mc5);
    t2.Add(mc6);
    t2.Add(mc7);
    t2.Add(mc8);
    t2.Add(mc9);

    L.Add(t2);

    var target1 = "123";
    var date = DateTime.Now.AddDays(502).Date;

    var result = L.SelectMany(x => x)
        .Where(y => y.SomeString == target1 && y.SomeDate.Date == date).Select(x=>x).ToList();

}

Upvotes: 0

Views: 1576

Answers (2)

Oxald
Oxald

Reputation: 837

You should avoid the usage of SelectMany since you are searching for a specific list:

var result = L
     .Where(l=>l.Any(y => y.SomeString == target1 && y.SomeDate.Date == date))
     .FirstOrDefault();

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460108

If you want the lists which contain at least one object that match your predicate:

var result = L.Where(list => list.Any(x => x.SomeString == target1 && x.SomeDate.Date == date)).ToList();

If you want only one list, replace the ToList with FirstOrDefault.

If you want the lists which contain at least one object that match your predicate and those list should only contain those objects, so all other non-matching objects are filtered out:

var result = L
    .Select(list => list.Where(x => x.SomeString == target1 && x.SomeDate.Date == date).ToList())
    .Where(list => list.Any())
    .ToList();

Upvotes: 1

Related Questions