从实体集合中删除重复项



>我有一个实体集合,其中包含来自不同营销列表的成员。因为一个人可以存在于各种营销列表中,所以我的实体集合可以多次包含同一个人。

为了最初对这些数据进行分组,我执行以下操作:

var groupedCustomerList = listMembers.Entities.GroupBy(u => u.Id).Select(grp => grp.ToList());

My EntityCollection 还包含一个名为"优先级"的属性,如果多次找到同一个人,则会导致以下情况

Group_1
- Person_1 (priority 1)
Group_2
- Person_1 (priority 2)
- Person_2 (priority 1)

我需要实现的是删除优先级较低的重复人员 -> Person_1 需要删除Group_2。

到目前为止,我尝试的是:

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

但这并没有给我想要的结果——>结果中总是同一个人

有人对我有提示吗?

将不胜感激。

提前谢谢你。

我刚刚基于实体框架在 c# 中创建了非常简单的控制台应用程序。

我创建了产品实体并添加了大约 15 个产品。 然后,我将所有这些实体产品添加到实体集合中。 所以现在我的要求几乎与你需要的相同。 我所做的是只保留我的实体集合中在产品 ID 中UnitsInStock最高的记录。

例如:对于Product ID1,我只记录了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; }
}
}

你可以试试这个。我使用了示例列表,但您可以申请实体集合。享受编码

List<Employee> employees = new List<Employee>();
var newEmployeList = employees.Distinct().ToList();

相关内容

  • 没有找到相关文章

最新更新