How to Lambda in Extension Methods with Multiple Lists?

Asked

Viewed 141 times

13

There are the following Objects:

public class Objeto1
{
   public List<Objeto2> PropObj1 {get; set;}
}

public class Objeto2
{
   public List<Objeto3> PropObj2 {get; set;}
}

public class Objeto3
{
   public int PropObj3 {get; set;}
}

I have a List of Object1 and I need to get a filtered list of Object 1, only those who possess the Propobj3 == 1:

List<Objeto1> listaObj1 = new List<Objeto1>();
// Suponhamos que listaObj1 já possui valores inseridos.

var resultado = listaObj1.Where(o1 => o1.PropObj1... ).ToList();

In this case, what would the expression lambda look like?
I’m using Entity Framework.

2 answers

10


If it’s Entity Framework, I think the list comes from a context, then I won’t use a separate list to answer. I will answer from the same context, because the construction of a list from a data context is completely different from the construction of a list from an operation in memory using Linq:

var lista = contexto.Objetos1
                  .Where(o1 => o1.PropObj1
                                 .Any(o2 => o2.PropObj2
                                              .Any(o3 => o3.PropObj3 == 1)))
                  .ToList();

In pure Linq, see that it would look different:

var sublist = list.PropObj1
                  .Where(o => o.PropObj2.Any(o2 => o2.PropObj3 == 1))
                  .ToList();

I rode you a Fiddle.

  • 1

    Thank you @Ciganomorrisonmendez, it worked perfectly. I didn’t know this .Any. You have no idea how much you’ve helped me!

8

I think this is what you want:

var resultado = listaObj1
    .SelectMany(o => o.PropObj1, (objeto1, objeto2) => new { objeto1, objeto2 })
    .SelectMany(o => o.objeto2.PropObj2, (objeto2, objeto3) => new { objeto2, objeto3 })
    .Where(o => o.objeto3.PropObj3 == 1).Select(o => new { o.objeto2.objeto1.PropObj1 })
    .ToList();

SelectMany() serves to transform list lists into a single sequential list.

Eventually you need to use one Select() to determine how either the result.

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • That answer returns Objeto3, and not Objeto1. Is incorrect.

  • Yeah, it doesn’t. I don’t know if I understand what he wants. If this is it, I’ll see what can be done.

  • It is quite common for AP’s to ask questions with "lambda" to build Linq expressions actually used in the Entity Framework, but Linq for the Entity Framework compiles an SQL sentence (in the case of SQL Server) or a BSON (in the case of Mongodb or other non-relational databases). In this case it is legal to always question the AP to see if what it wants is a list operation or an EF sentence. I made a Fork of your Fiddle. See that he returns one Objeto3.

  • Yes, I agree. I just don’t know what outcome he expects.

  • @Ciganomorrisonmendez I think it is now in agreement with what he wants. Yours is better but is as an alternative. Maybe I still need some adaptation on select then I’ll fix it.

  • I’ve never tested this way. I’ll even check this alternative. It might be good for other scenarios.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.