合并两个列表中的属性

时间:2015-07-22 13:02:37

标签: c# asp.net-mvc-4 model-view-controller controller

我有这段代码:

   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中的三个属性实现到结果列表中。是否有一种优雅的方式来做到这一点?

1 个答案:

答案 0 :(得分:-1)

评论员是正确的,你可以使用LINQ的JOIN功能来&#34;合并&#34;这两个列表在一起,例如:

            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();