Reputation: 207
I have an EntityCollection which contains members coming from different Marketing Lists. Because a person can exist in various MarketingLists my EntityCollection can contain the same person multiple times.
To have this data initially grouped I do the following:
var groupedCustomerList = listMembers.Entities.GroupBy(u => u.Id).Select(grp => grp.ToList());
My EntityCollection contains also an Attribute called "priority" which leads to the following if the same person is found multiple times
Group_1
- Person_1 (priority 1)
Group_2
- Person_1 (priority 2)
- Person_2 (priority 1)
What I need to achieve is to remove the duplicate person(s) with the lower priority -> Person_1 in Group_2 needs to be removed.
What I have tried so far is:
foreach (var item in groupedCustomerList)
{
if (item.Count > 1)
{
// order the items in the group by priority set in the SelectionRow and take the first
// entry with the highest priority
var element = item.OrderBy(o => o.Attributes[AttributeNames.SelectionRow.SelectionRowPriority]).Take(1).ToList();
listMembersConsolidated.Add(element[0]);
}
else
{
listMembersConsolidated.Add(item[0]);
}
}
But this does not give me the desired result -> always the same person in the result
Does anybody have a hint for me?
Would be highly appreciated.
Thank you in advance.
Upvotes: 0
Views: 1476
Reputation: 5531
I just created very simple console application in c# based on entity framework.
I created Product Entity and added around 15 products.
Then I added all these entity products in an Entity collection.
So that now I have almost same req as you need.
What I did is that keep only those Records in my entitycollection which have UnitsInStock
highest among a Product ID.
For ex: For Product ID
1 I have only taken record with UnitsInStock
=40
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Objects.DataClasses;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityCollectionTesting
{
class Program
{
static void Main(string[] args)
{
var productList =
new List<Product> {
new Product { ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 },
new Product { ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 },
new Product { ProductID = 3, ProductName = "Aniseed Syrup", Category = "Condiments", UnitPrice = 10.0000M, UnitsInStock = 13 },
new Product { ProductID = 4, ProductName = "Chef Anton's Cajun Seasoning", Category = "Condiments", UnitPrice = 22.0000M, UnitsInStock = 53 },
new Product { ProductID = 5, ProductName = "Chef Anton's Gumbo Mix", Category = "Condiments", UnitPrice = 21.3500M, UnitsInStock = 0 },
new Product { ProductID = 6, ProductName = "Grandma's Boysenberry Spread", Category = "Condiments", UnitPrice = 25.0000M, UnitsInStock = 120 },
new Product { ProductID = 7, ProductName = "Uncle Bob's Organic Dried Pears", Category = "Produce", UnitPrice = 30.0000M, UnitsInStock = 15 },
new Product { ProductID = 8, ProductName = "Northwoods Cranberry Sauce", Category = "Condiments", UnitPrice = 40.0000M, UnitsInStock = 6 },
new Product { ProductID = 9, ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = 29 },
new Product { ProductID = 10, ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = 31 },
new Product { ProductID = 1, ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = 40 },
new Product { ProductID = 2, ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = 56 },
new Product { ProductID = 3, ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = 11 },
new Product { ProductID = 4, ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = 12 },
new Product { ProductID = 5, ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = 1 }
};
EntityCollection<Product> entityCollection = new EntityCollection<Product>();
EntityCollection<Product> newCollection = new EntityCollection<Product>();
foreach (var VARIABLE in productList)
{
entityCollection.Add(VARIABLE);
newCollection.Add(VARIABLE);
}
foreach (var ec in entityCollection)
{
foreach (var nc in newCollection)
{
if (ec.ProductID == nc.ProductID)
{
if (ec.UnitsInStock > nc.UnitsInStock)
{
newCollection.Remove(nc);
break;
}
}
}
}
foreach (var VARIABLE in newCollection)
{
Console.WriteLine($"{VARIABLE.ProductID} and {VARIABLE.UnitsInStock}");
}
Console.ReadLine();
}
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public string Category { get; set; }
public decimal UnitPrice { get; set; }
public int UnitsInStock { get; set; }
}
}
Upvotes: 0
Reputation: 26
You can try this. I've used sample list but you can apply with your entity collection. Enjoy coding
List<Employee> employees = new List<Employee>();
var newEmployeList = employees.Distinct().ToList();
Upvotes: -1