#c# #.net #linq
#c# #.net #linq
Вопрос:
У меня есть два списка
List A (CG300019159, CG300018158, FS300076458)
List B Returns grouped lists like below
{
"pallet_identifier": "CG300018158",
"shipment_items": [
{
"sku": "10366960",
"stock_qty": 12,
"description": "MOTHERCARE CREAM BABYGROW W/HAT"
},
{
"sku": "10346788",
"stock_qty": 1,
"description": "KIT 7PC ESS CREW NECK CARDIGAN SKY BLUE"
}
]
},
"pallet_identifier": "CG300018187",
"shipment_items": [
{
"sku": "10366960",
"stock_qty": 12,
"description": "MOTHERCARE CREAM BABYGROW W/HAT"
},
{
"sku": "10346788",
"stock_qty": 1,
"description": "KIT 7PC ESS CREW NECK CARDIGAN SKY BLUE"
}
]
},
Я хочу удалить поддон и его содержимое из списка B, используя свойство pallet_identifier.
Пробовал, как показано ниже
entity = new List<GoodInWarehouseBM>((from consighdrs in mi9db.consighdrs
join consigdests in mi9db.consigdests on consighdrs.consignment equals consigdests
.consignment
join consigliness in mi9db.consiglines on consigdests.condestint equals consigliness
.condestint
join productcodess in mi9db.productcodes on consigliness.varint equals productcodess.varint
join products in mi9db.products on productcodess.prodint equals products.prodint
where consigdests.destination == storeId amp;amp; consighdrs.status == "T" amp;amp; consighdrs.warehouse == "900"
group new { consigdests, productcodess, consigliness, products } by consigdests.consignment into grp
select new GoodInWarehouseBM
{
pallet_identifier = grp.Key,
shipment_items = grp.Select(a => new GoodInWarehouseBM.ShipmentItems
{
sku = a.productcodess.variantcode,
stock_qty = a.consigliness.issueqty,
description = a.products.proddesc
}).ToList()
}).ToList().RemoveAll(i => !getConsignmentbookedin.Contains(i.pallet_identifier)));
Ответ №1:
Вы можете попробовать использовать .Where(i=> getConsignmentbookedin.Any(z=> z != i.pallet_identifier))
entity = new List<GoodInWarehouseBM>((from consighdrs in mi9db.consighdrs
join consigdests in mi9db.consigdests on consighdrs.consignment equals consigdests
.consignment
join consigliness in mi9db.consiglines on consigdests.condestint equals consigliness
.condestint
join productcodess in mi9db.productcodes on consigliness.varint equals productcodess.varint
join products in mi9db.products on productcodess.prodint equals products.prodint
where consigdests.destination == storeId amp;amp; consighdrs.status == "T" amp;amp; consighdrs.warehouse == "900"
group new { consigdests, productcodess, consigliness, products } by consigdests.consignment into grp
select new GoodInWarehouseBM
{
pallet_identifier = grp.Key,
shipment_items = grp.Select(a => new GoodInWarehouseBM.ShipmentItems
{
sku = a.productcodess.variantcode,
stock_qty = a.consigliness.issueqty,
description = a.products.proddesc
}).ToList()
}).Where(i=> getConsignmentbookedin.Any(z=> z != i.pallet_identifier)).ToList();
Комментарии:
1. что делает . Любое (i.pallet_identifier) действие здесь недопустимо, pallet_identifer — это строка, а не функция
2. Это выглядит так, как будто это должно сработать, но на самом деле все равно возвращает идентификаторы pallet_identifiers, которые находятся в списке A.
3. Изменено на . Где(i => !getConsignmentbookedin. Любой (z => z != i.pallet_identifier)). ToList()); и тогда это сработало.
Ответ №2:
Вы должны применить фильтр перед group и select, это улучшит ваш запрос на несколько величин, поскольку вы отбрасываете все, что вам не нужно.
entity = new List<GoodInWarehouseBM>((from consighdrs in mi9db.consighdrs.Where(i=> !getConsignmentbookedin.Contains(i.Id(id of item not sure which one it is))
join consigdests in mi9db.consigdests on consighdrs.consignment equals consigdests
.consignment
join consigliness in mi9db.consiglines on consigdests.condestint equals consigliness
.condestint
join productcodess in mi9db.productcodes on consigliness.varint equals productcodess.varint
join products in mi9db.products on productcodess.prodint equals products.prodint
where consigdests.destination == storeId amp;amp; consighdrs.status == "T" amp;amp; consighdrs.warehouse == "900"
group new { consigdests, productcodess, consigliness, products } by consigdests.consignment into grp
select new GoodInWarehouseBM
{
pallet_identifier = grp.Key,
shipment_items = grp.Select(a => new GoodInWarehouseBM.ShipmentItems
{
sku = a.productcodess.variantcode,
stock_qty = a.consigliness.issueqty,
description = a.products.proddesc
}).ToList()
})).ToList();