Property in class IEnumerable <TDto> get TDto.A == a

So I have a class similar to this

public class Order
{
    //some other stuff ... 
    //setting the internal _orderItems collection ...
    IEnumerable<OrderItems> OrderItems { get { return _orderItems; }
}

public class OrderItem
{
    //other stuff
    public string ProductName {get; set;}
}

If I have a set of orders of a certain type and I have access via linq to order, something like

 myOrderRespository.Where(x=>x.OrderItems)

Then I have access to getEnumerator, waht, I would like it to be able to do something like

 myOrderRespository.Where(x=>x.OrderItems.ProductName == "Blah")

Is it possible? this is a compiled script and its pseudo-code that I am trying to simplify, so it’s easy to explain (so please forgive me if there are a few errors)

+3
source share
1 answer

You are probably looking for something like:

var results = myOrderRepository.Where(x => x.OrderItems.Any(item => item.ProductName == "Blah"));

This will return all instances Orderwith at least one OrderItem that has a ProductName from "Blah."

+3
source

Source: https://habr.com/ru/post/1738061/


All Articles