简体   繁体   中英

C# Net Core Compare 2 List with Linq and Create New One

I'm using .NET Core 6, SQL Server and Oracle databases. I created 2 object type generic lists. All lists contains same kind information.

Line, MaterialName, MaterialId and MaterialCount information and I don't have id information. I'm unable to join

var productList = new List<object>(); // **used elements**
var reportList = new List<object>();  // **must be used elements**

I have 4 lines and 20 materials. reportList has 40 elements, productList has 21.

I need to calculate percentile of materials. I need to proportion the materials used in an assembly line to the materials used as a percentage. The example below should use 2500 motherboards, but it looks like 2000 was used. So 100 * 2000 / 2500 = 80. So the efficiency is 80%.

Examples:

  reportList element                                     productList element

  {                                                      {
    "materialId": 1,                                            "materialId": 1,
    "line": "Line1",                                            "line": "Line1",
    "materialName": "Mainboard",                                "materialName": "Mainboard",
    "materialCount": 2500                                       "materialCount": 2000
  },                                                      },

Final list element have to be:

{  
    "materialId": 1,
    "line": "Line1",
    "materialName": "Mainboard",
    "materialCount": 80
},

If a product has never been used, it will not be registered in the productlist . The percentage number will automatically be 0.(So materialCount must be 0. materialCount = 0). So final list elements count will be same with reportList.

What doesn't work? They are simple generic lists. After "." symbol, we can't use any information because they are list. I can't type something is equal to something. We need something different...

*from report in reportList

join product in productList
on report.Line equals generalRule.Line* 

Assuming, you are using Linq to objects, you can use the moreLinq library

It has a LeftJoin extension method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM