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

Time:03-09

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* 

CodePudding user response:

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

CodePudding user response:

Sorry for misunderstanding.Thanks a lot.It works great for now... Only Wemod Kart seems odd and it's value 0 but i can tolerate it.Thanks Stackoverflow family...We saved the day...

{
    "materialId": 9,
    "line": "MONTAJ2",
    "materialName": "Wemod Kart",
    "materialCount": 110
  },
  {
    "materialId": 1,
    "line": "MONTAJ1",
    "materialName": "Gövde",
    "materialCount": 58
  },
  {
    "materialId": 4,
    "line": "MONTAJ1",
    "materialName": "Kablo Ağacı",
    "materialCount": 81
  },
  {
    "materialId": 6,
    "line": "MONTAJ1",
    "materialName": "Isıtıcı",
    "materialCount": 17
  },
  {
    "materialId": 7,
    "line": "MONTAJ1",
    "materialName": "Sirk. Motoru",
    "materialCount": 82
  },
  {
    "materialId": 8,
    "line": "MONTAJ1",
    "materialName": "Parazit Filtresi",
    "materialCount": 75
  },
  {
    "materialId": 12,
    "line": "MONTAJ1",
    "materialName": "Main Board",
    "materialCount": 8
  },
  {
    "materialId": 15,
    "line": "MONTAJ1",
    "materialName": "Tahliye Motoru",
    "materialCount": 0
  },
  {
    "materialId": 16,
    "line": "MONTAJ1",
    "materialName": "Fan Motoru",
    "materialCount": 54
  },
  {
    "materialId": 18,
    "line": "MONTAJ1",
    "materialName": "Şebeke Kablosu",
    "materialCount": 0
  },
  {
    "materialId": 21,
    "line": "MONTAJ1",
    "materialName": "Kablo Ağacı",
    "materialCount": 30
  },
  {
    "materialId": 1,
    "line": "MONTAJ2",
    "materialName": "Gövde",
    "materialCount": 0
  },
  {
    "materialId": 4,
    "line": "MONTAJ2",
    "materialName": "Kablo Ağacı",
    "materialCount": 85
  },
  {
    "materialId": 6,
    "line": "MONTAJ2",
    "materialName": "Isıtıcı",
    "materialCount": 18
  },
  {
    "materialId": 7,
    "line": "MONTAJ2",
    "materialName": "Sirk. Motoru",
    "materialCount": 81
  },
  {
    "materialId": 8,
    "line": "MONTAJ2",
    "materialName": "Parazit Filtresi",
    "materialCount": 35
  },
  {
    "materialId": 12,
    "line": "MONTAJ2",
    "materialName": "Main Board",
    "materialCount": 0
  },
  {
    "materialId": 15,
    "line": "MONTAJ2",
    "materialName": "Tahliye Motoru",
    "materialCount": 0
  },
  {
    "materialId": 16,
    "line": "MONTAJ2",
    "materialName": "Fan Motoru",
    "materialCount": 37
  },
  {
    "materialId": 18,
    "line": "MONTAJ2",
    "materialName": "Şebeke Kablosu",
    "materialCount": 0
  },
  {
    "materialId": 21,
    "line": "MONTAJ2",
    "materialName": "Kablo Ağacı",
    "materialCount": 15
  },
  {
    "materialId": 2,
    "line": "MONTAJ4",
    "materialName": "Elektronik Kart",
    "materialCount": 0
  },
  {
    "materialId": 4,
    "line": "MONTAJ4",
    "materialName": "Kablo Ağacı",
    "materialCount": 0
  },
  {
    "materialId": 6,
    "line": "MONTAJ4",
    "materialName": "Isıtıcı",
    "materialCount": 0
  },
  {
    "materialId": 7,
    "line": "MONTAJ4",
    "materialName": "Sirk. Motoru",
    "materialCount": 0
  },
  {
    "materialId": 8,
    "line": "MONTAJ4",
    "materialName": "Parazit Filtresi",
    "materialCount": 0
  },
  {
    "materialId": 15,
    "line": "MONTAJ4",
    "materialName": "Tahliye Motoru",
    "materialCount": 0
  },
  {
    "materialId": 16,
    "line": "MONTAJ4",
    "materialName": "Fan Motoru",
    "materialCount": 0
  },
  {
    "materialId": 18,
    "line": "MONTAJ4",
    "materialName": "Şebeke Kablosu",
    "materialCount": 0
  },
  {
    "materialId": 20,
    "line": "MONTAJ4",
    "materialName": "Display Kablo",
    "materialCount": 0
  },
  {
    "materialId": 1,
    "line": "MONTAJ3",
    "materialName": "Gövde",
    "materialCount": 1
  },
  {
    "materialId": 4,
    "line": "MONTAJ3",
    "materialName": "Kablo Ağacı",
    "materialCount": 0
  },
  {
    "materialId": 6,
    "line": "MONTAJ3",
    "materialName": "Isıtıcı",
    "materialCount": 1
  },
  {
    "materialId": 7,
    "line": "MONTAJ3",
    "materialName": "Sirk. Motoru",
    "materialCount": 0
  },
  {
    "materialId": 8,
    "line": "MONTAJ3",
    "materialName": "Parazit Filtresi",
    "materialCount": 0
  },
  {
    "materialId": 12,
    "line": "MONTAJ3",
    "materialName": "Main Board",
    "materialCount": 0
  },
  {
    "materialId": 15,
    "line": "MONTAJ3",
    "materialName": "Tahliye Motoru",
    "materialCount": 0
  },
  {
    "materialId": 16,
    "line": "MONTAJ3",
    "materialName": "Fan Motoru",
    "materialCount": 0
  },
  {
    "materialId": 18,
    "line": "MONTAJ3",
    "materialName": "Şebeke Kablosu",
    "materialCount": 0
  },
  {
    "materialId": 21,
    "line": "MONTAJ3",
    "materialName": "Kablo Ağacı",
    "materialCount": 0
  }
  • Related