我有一個奇怪的要求。我根據某些標準對記錄進行分組(例如,Gln和其他字段......)。對於每個組,我都有進一步的對象列表。我意識到來自這些列表的幾個對象必須是分開的,並且需要使用相同的Gln複製另一個組。因爲這些物體不應該放置在那裏。他們必須制定另一組具有相同密鑰的組織(即Gln)。如何從父母和子組中使用Linq提取子組
請看下面的代碼。
var claimsQuery = (from consignment in Consignments
let consigneeAddress = consignment.ConsignmentAddresses.FirstOrDefault(pr => pr.AddressTypeId == 2)
where (consignment.ClientId == 26 && consignment.FixDate != null)
group consignment by new
{
consigneeAddress.GLN,
consignment.ForwarderId,
consigneeAddress.Country.ISO,
consignment.FixDate,
ProductName = Products.FirstOrDefault(pr => pr.ProductId == consignment.ProductId).Name,
ProductId = consignment.ProductId
}
into g
select new DepoOverviewData
{
ForwarderId = g.Key.ForwarderId,
FixDate = g.Key.FixDate.GetValueOrDefault(),
Gln = g.Key.GLN,
ProductName = g.Key.ProductName,
ConsignmentsOverviewData =
(from singleConsignment in Consignments.Where(pr => (pr.FixDate == g.Key.FixDate) && (pr.ProductId == g.Key.ProductId))
join x in ConsignmentAddresses on singleConsignment.ConsignmentId equals x.ConsignmentId
where x.GLN == g.Key.GLN && x.AddressTypeId == 2
select new ConsignmentClaimsOverviewData()
{
ConsignmentId = singleConsignment.ConsignmentId,
Weight = singleConsignment.Weight,
ChargeableWeight = singleConsignment.ChargeableWeight,
TotalCost = singleConsignment.NetTotalCost,
ProductId = singleConsignment.ProductId,
ProductName = Products.FirstOrDefault(pr => pr.ProductId == singleConsignment.ProductId).Name,
FixDate = singleConsignment.FixDate,
ExpressTime = singleConsignment.ExpressTime,
ClientOrderNumber = singleConsignment.ClientOrderNumber,
DeliveryDate = singleConsignment.DeliveryDate,
InTime = false, // this will be reset later. (i.e True, false or null after iterating again over each group).
}).ToList()
}).ToList();
如果您創建一個較小的輸入示例和所需的輸出 –