[英]C# Net Core Compare 2 List with Linq and Create New One
我正在使用 .NET Core 6、SQL 服務器和 Oracle 數據庫。 我創建了 2 個 object 類型的通用列表。 所有列表都包含同類信息。
Line、MaterialName、MaterialId 和 MaterialCount 信息,我沒有 id 信息。 我無法加入
var productList = new List<object>(); // **used elements**
var reportList = new List<object>(); // **must be used elements**
我有 4 條線和 20 種材料。 reportList
有 40 個元素, productList
有 21 個。
我需要計算材料的百分位數。 我需要將裝配線中使用的材料與使用的材料按百分比進行比例分配。 下面的示例應該使用 2500 個主板,但看起來使用了 2000 個。 所以 100 * 2000 / 2500 = 80。所以效率是 80%。
例子:
reportList element productList element
{ {
"materialId": 1, "materialId": 1,
"line": "Line1", "line": "Line1",
"materialName": "Mainboard", "materialName": "Mainboard",
"materialCount": 2500 "materialCount": 2000
}, },
最終列表元素必須是:
{
"materialId": 1,
"line": "Line1",
"materialName": "Mainboard",
"materialCount": 80
},
如果從未使用過產品,則不會在產品列表中productlist
。 百分比數將自動為 0。(因此 materialCount 必須為 0。materialCount = 0)。 所以最終的列表元素計數將與 reportList 相同。
什么不起作用? 它們是簡單的通用列表。 后 ”。” 符號,我們不能使用任何信息,因為它們是列表。 我無法輸入 something is equal to something。 我們需要一些不同的東西...
*from report in reportList
join product in productList
on report.Line equals generalRule.Line*
假設,您正在使用 Linq 對象,您可以使用moreLinq 庫
它有一個LeftJoin
擴展方法。
class Element
{
public int materialId { get; set; }
public string line { get; set; }
public string materialName { get; set; }
public double materialCount { get; set; } // Must be double here for the calculation to work
}
var productList = new List<Element>(); // **used elements**
var reportList = new List<Element>(); // **must be used elements**
var result = reportList
.LeftJoin(
productList,
// Join by materialId, line and materialName
r => new { materialId = r.materialId, line = r.line, materialName = r.materialName },
// No element in productList found, materialCount will be 0
r => new Element {materialId = r.materialId, line = r.line, materialName = r.materialName },
// An element in productList was found => do the calculation
(r, p) => new Element {materialId = r.materialId, line = r.line, materialName = r.materialName, materialCount = 100 * p.materialCount / r.materialCount });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.