[英]merge properties from two lists
我有以下代码:
IEnumerable<CarResultsViewModel> priceResults = price.Select(p => new CarResultsViewModel
{
PricePerDay = p.PricePerDay,
PricePerDayDelayed = p.PricePerDayDelayed,
TotalPrice = (double)p.Total_price
}).ToList();
IEnumerable<CarResultsViewModel> results = car.Select(c => new CarResultsViewModel
{
CreationYear = c.CreationYear,
ManufacturerName = c.ManufacturerName,
ModelName = c.ModelName,
Gear = c.Gear,
CurrentKm = c.CurrentKM,
Picture = c.Picture,
StartDate = model.StartDate,
ReturnDate = model.ReturnDate
}).ToList();
我想将priceResults中的三个属性实现到结果列表中。 有什么优雅的方法吗?
评论者是正确的,可以使用LINQ的JOIN函数将这两个列表“合并”在一起,例如:
var carResults = (from pr in priceResults
join r in results
on pr.CarId equals r.CarId
select new CarResultsViewModel
{
CarId = pr.CarId,
PricePerDay = pr.PricePerDayDelayed,
ManufacturerName = r.ManufacturerName,
etc...
}).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.