문제

Say I have this structure:

public class Car
{
   public string Name;
   public IList<Tire> Tires;
}

public class Tire
{
   public string Name;
   public int Size;
}

I want a query to return all Cars that have Tires with size 40.

I am thinking this way, what am I missing?

Cars.Where(x => x.Tires.Where(y => y.Size == 40));

This code throws this error: "Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type"

도움이 되었습니까?

해결책

You want

Cars.Where(x => x.Tires.Any(y => y.Size == 40));

or

Cars.Where(x => x.Tires.All(y => y.Size == 40));

Depending on the requirement.

Your version won't work because the outer lambda is actually returning an IEnumerable<Tire>, whereas it needs to be a bool.

다른 팁

You should use Any instead.

Cars.Where(x => x.Tires.Any(y => y.Size == 40));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top