I have orders
collection that contains products
collection. I 'm pass some product ids as list to the method. 목록의 ID와 일치하는 제품 목록을 반환해야합니다 .I need to return a list of products matching with any of the id in the input
list.
이와 같은 foreach 루프를 사용해야합니까? 조언 부탁드립니다 ..
public List < ProductOrderData > ListProductsByOrderId(List < Guid > input) {
List < ProductOrderData > products = new List < ProductOrderData > ();
foreach(var id in input) {
var orders = this.Collection.AsQueryable().SelectMany(order => order.Products.Where(product => product.Id == id));
}
}
업데이트 3 : 제품 ID를 통과했으며 제품 ID와 일치하는 주문에서 제품 목록을 가져와야합니다.
제품 컬렉션이 포함 된 주문 컬렉션이 있습니다.
좋습니다. 다음과 같은 것이 있습니다.
var ordersWithTheirProducts = ...
.Select(... => new Order
{
// some Order properties:
Id = ...
OrderDate = ...
ClientId = ...
// Products of this Order:
Products = ...
.Select( product => new Product
{
// Several Product properties
Id = product.Id,
Name = product.Name,
Price = product.Price,
...
},
};
일부 제품 ID를 목록으로 전달하고 있습니다.
IEnumerable<Guid> productIds = ...
입력 목록에있는 ID와 일치하는 제품 목록을 반환해야합니다.
따라서 주문에 사용 된 모든 제품을 선택하고 해당 제품에 ProductIds의 값 중 하나 인 ID가있는 경우 결과에 포함하려고합니다.
먼저 주문에 사용 된 모든 제품을 추출한 다음 중복 항목을 제거하고 마지막으로 제품 ID에 ID가있는 제품 만 보관합니다.
var result = Orders.SelectMany(order => order.Products) // extract all Products
.Distinct() // remove duplicate products
.Where(product => productIds.Contains(product.Id))
// keep only those products with an Id that is in productIds
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다