C#在两个列表中找到最低价格
作为大学项目的一部分,我目前正在开发一个MVC Web应用程序。我正在从两个不同的Web API微服务中检索两组“产品”,它们都有完全相同的“产品”,但价格不同,它们都使用以下数据模型:C#在两个列表中找到最低价格,c#,asp.net-mvc,C#,Asp.net Mvc,作为大学项目的一部分,我目前正在开发一个MVC Web应用程序。我正在从两个不同的Web API微服务中检索两组“产品”,它们都有完全相同的“产品”,但价格不同,它们都使用以下数据模型: public int BrandId { get; set; } public string BrandName { get; set; } public int CategoryId { get; set; } public string CategoryName { get; set; } public s
public int BrandId { get; set; }
public string BrandName { get; set; }
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public string Ean { get; set; }
public int Id { get; set; }
public bool InStock { get; set; }
public string Name { get; set; }
public double Price { get; set; }
我将两个API的结果存储在两个列表中,但需要一种方法来比较和提取价格最低的API中的产品,然后将其存储在单独的列表中
还值得注意的是,“EAN”字符串在两个API中也是相同的
我该怎么做呢
谢谢。假设
Id
是两个API共用的唯一标识符,您可以将这两个列表连接起来,按Id
对它们进行分组,按价格
对每组进行订购,然后选择价格较低的一个:
var lowerPriced = allProductsOne.Concat(allProductsTwo)
.GroupBy(p => p.Id)
.Select(g => g.OrderBy(p => p.Price).First())
.ToList();
好的,您可以通过比较两个列表中的每个实体,找到您需要的实体。