Goal: I am sure there is a better way of doing this and would appreciate this learning opportunity.
I have a list of products and want to find all duplicate products in the list, then mark the status field in the list with -1 to signal duplicate record found. This is based on two or more columns in the list currently I have three columns but would like to build a helper on the the columns that them as duplicate. The rules are to check by Name, Category and Weight if there are duplicates in the list mark the Status field with -1 for the duplicate record. (
The following code works correctly and returns the correct result, but I feel there are other cleaner way of doing this than my code. I welcome any and all suggestions.
Currently it is hard coded to work with this example but I am confident this could be done with a helper or extension method that would be more generic and useful.
public class Product
{
public string Name { get; set; }
public string Category { get; set; }
public char Weight { get; set; }
public int Status { get; set; }
public string Location {get;set;}
}
public enum ImportStatus
{
Duplicate = -1,
AwaitingProcess = 0,
}
void Main()
{
// you need to fix the case sensitivity
// *** Attention ***
List<Product> ProductList = new List<Product>()
{
new Product(){Name="Ginger",Category="Fresh", Weight= 'B',Status= 0, Location="Produce"}
,new Product(){Name="Ginger",Category="Dry", Weight= 'A',Status= 0, Location="Front Counter"}
,new Product(){Name="LEMON",Category="Fruit", Weight= 'B',Status= 0, Location="Name Area"}
,new Product(){Name="LEMON",Category="Fruit", Weight= 'B',Status= 0, Location="Outer Court"}
,new Product(){Name="lettuce",Category="Produce", Weight='X',Status= 0, Location="Produce"}
,new Product(){Name="Lettuce",Category="Product", Weight='X',Status= 0, Location="Freezer"}
,new Product(){Name="Apple",Category="Fruit", Weight= 'S',Status= 0, Location="Product"}
,new Product(){Name="Pine Apple",Category="Fruit", Weight= 'S',Status= 0, Location="Front Counter"}
};
List<Product> filteredProductList =ProductList
.Where (l => l.Status==0)
.GroupBy (r=>new { r.Name,r.Category,r.Weight},( grp, tbl)=> new{GROUP=grp,TBL=tbl})
.Where (r => r.TBL.Count()>1)
.SelectMany(r=>r.TBL)
.Select(r=>new Product { Name=r.Name,Category=r.Category,Weight= r.Weight,Location=r.Location,Status=(int)ImportStatus.Duplicate})
.Union(ProductList
.Where (l => l.Status==0)
.GroupBy (r=>new { r.Name,r.Category,r.Weight},( grp, tbl)=> new{GROUP=grp,TBL=tbl})
.Where (r => r.TBL.Count()==1)
.SelectMany(r=>r.TBL)
.Select (r =>new Product{ Name=r.Name,Category=r.Category,Weight= r.Weight,Location=r.Location,Status=0})
)Here is the desired output
<div id="final"> <div class="spacer">| Name | Category | Weight | StatusΞΞ | Location |
|---|---|---|---|---|
| LEMON | Fruit | B | -1 | Name Area |
| LEMON | Fruit | B | -1 | Outer Court |
| Ginger | Fresh | B | 0 | Produce |
| Ginger | Dry | A | 0 | Front Counter |
| lettuce | Produce | X | 0 | Produce |
| Lettuce | Product | X | 0 | Freezer |
| Apple | Fruit | S | 0 | Product |
| Pine Apple | Fruit | S | 0 | Front Counter |