繁体   English   中英

从EntityCollection中删除重复项

[英]Remove duplicates from EntityCollection

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

要将这些数据初始分组,请执行以下操作:

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

我的EntityCollection还包含一个称为“优先级”的属性,如果多次发现同一个人,则会导致以下问题

Group_1
- Person_1 (priority 1)

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

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

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

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种产品。 然后,我将所有这些实体产品添加到Entity集合中。 因此,现在我的需求几乎与您相同。 我所做的是仅将在实体ID中具有UnitsInStock最高记录的记录保留在我的实体集合中。

例如:对于Product ID 1,我仅使用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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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