通过 id 将两个不同的列表加入一个列表

join two different list by id into one list

我有两个不同的列表,其中包含两个不同的对象。然后我得到了一个视图模型列表,其中包含两个对象的属性,我希望将它们加入该列表。

    //Product
    public string id { get; set; }
    public string unitMeasurement { get; set; }
    public Nullable<int> minOrderQty { get; set; }
    public Nullable<int> packSize { get; set; }
    public string leadTime { get; set; }
    public Nullable<int> generalAccessoryCategoryId { get; set; }
    public string Company { get; set; }
    public Nullable<decimal> Weight { get; set; }
    public Nullable<int> ProductType { get; set; }

    //ProductDescription
    public string id { get; set; }
    public string language { get; set; }
    public string shortDescription { get; set; }
    public string detailDescription { get; set; }
    public string Name { get; set; }
    public Nullable<int> Rank { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public Nullable<System.DateTime> EndDate { get; set; }


     public class ProductResponse
{

    public string id { get; set; }

    //Product
    public string unitMeasurement { get; set; }
    public Nullable<int> minOrderQty { get; set; }
    public Nullable<int> packSize { get; set; }
    public string leadTime { get; set; }
    public Nullable<int> generalAccessoryCategoryId { get; set; }
    public string Company { get; set; }
    public Nullable<decimal> Weight { get; set; }
    public Nullable<int> ProductType { get; set; }

    //ProductDescription

    public string language { get; set; }
    public string shortDescription { get; set; }
    public string detailDescription { get; set; }
    public string Name { get; set; }
    public Nullable<int> Rank { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }
    public Nullable<System.DateTime> EndDate { get; set; }
}

所以我只想将产品列表和产品描述列表加入到 productResponse 列表中。我怎样才能做到这一点?产品 ID 和产品描述相同,这就是我想加入的原因。

通过 id 加入他们,然后拨打 ToList:

var productResponses = from p in products 
                       join pd in productDescriptions
                       on p.id equals pd.id
                       select new ProductResponse
                       { 
                          id = p.id,
                          language = pd.language,
                          // ...
                       }
var list = productResponses.ToList();