0

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

要对这些数据进行初始分组,我执行以下操作:

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

我的 EntityCollection 还包含一个名为“priority”的属性,如果多次找到同一个人,则会导致以下结果

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

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

有人对我有提示吗?

将不胜感激。

先感谢您。

4

2 回答 2

0

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

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

例如:对于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; }
    }
}
于 2019-08-30T11:41:08.970 回答
-1

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

List<Employee> employees = new List<Employee>();
var newEmployeList = employees.Distinct().ToList();
于 2019-08-29T09:17:45.880 回答