简体   繁体   English

如何:选择不同的对象 <T> 来自多个列表 <T> 具有1个或多个相同类型的属性

[英]How to: Select Distinct objects<T> from multiple Lists<T> with 1 or more properties of same type

So this is a design question that has vexed me for hours, and I have to reach out to the group for an assist. 因此,这是一个设计问题,已经困扰了我几个小时,因此我必须向小组寻求帮助。 I have a collection with thousands of shared entities, and need to retrieve a distinct list of managers from three different properties contained in two lists, two managers in the Stores list, and one manager from warehouse collection. 我有一个包含数千个共享实体的集合,需要从两个列表中包含的三个不同属性,商店列表中的两个经理以及仓库集合中的一个经理中检索不同的经理列表。

To simplify the problem, I've written a simple console program that highlights the challenge. 为了简化问题,我编写了一个简单的控制台程序来强调这一挑战。 I tossed it together, so yes, I know it's inefficient, but it demonstrates the problem: 我把它扔在一起,所以是的,我知道它效率低下,但它说明了问题:

    public class Program
    {
        static void Main(string[] args)
        {
            DistributionGroup d = new DistributionGroup();
            Console.WriteLine("====Store Managers====");
            foreach(Manager m in d.Stores.Select(m => m.StoreManager).Distinct())
            {
                Console.WriteLine("{0}:{1}", m.Id, m.Name);
            }

            Console.WriteLine("=====Inv. Managers=====");
            foreach (Manager m in d.Stores.Select(m => m.InventoryManager).Distinct())
            {
                Console.WriteLine("{0}:{1}", m.Id, m.Name);
            }

            Console.WriteLine("===Warehouse Managers===");
            foreach (Manager m in d.Warehouses.Select(m => m.WarehouseManager).Distinct())
            {
                Console.WriteLine("{0}:{1}", m.Id, m.Name);
            }                
        }
    }
    public class DistributionGroup
    {
        private Manager m1 = new Manager(1, "Bob Wilson");
        private Manager m2 = new Manager(2, "Chris Warren");
        private Manager m3 = new Manager(3, "Mike Olsen");
        private Manager m4 = new Manager(4, "Aaron Erikson");

        public List<RetailStore> Stores;
        public List<Warehouse> Warehouses;

        public DistributionGroup()
        {
            RetailStore s1 = new RetailStore(1, m1, m1);
            RetailStore s2 = new RetailStore(2, m1, m3);
            RetailStore s3 = new RetailStore(3, m2, m2);
            Warehouse w1 = new Warehouse(1, m4);
            Warehouse w2 = new Warehouse(2, m2);

            Stores = new List<RetailStore>();
            Stores.Add(s1);
            Stores.Add(s2);
            Stores.Add(s3);

            Warehouses = new List<Warehouse>();
            Warehouses.Add(w1);
            Warehouses.Add(w2);
        }
    }

    public class Manager
    {
        public int Id;
        public string Name;
        public Manager(int id, string name)
        {
            Id = id; Name = name;
        }
    }
    public class RetailStore
    {
        public int Id;
        public Manager StoreManager;
        public Manager InventoryManager;
        public RetailStore(int id, Manager mgr, Manager inventoryMgr)
        {
            Id = id;
            StoreManager = mgr;
            InventoryManager = inventoryMgr;
        }
    }

    public class Warehouse
    {
        public int Id;
        public Manager WarehouseManager;
        public Warehouse(int id, Manager mgr)
        {
            Id = id;
            WarehouseManager = mgr;
        }
    }

What I need to do is generate a distinct list of Managers from all three properties: 我需要做的是从所有三个属性生成一个不同的Manager列表:

  1. RetailStore.StoreManager RetailStore.StoreManager
  2. RetailStore.InventoryManager RetailStore.InventoryManager
  3. Warehouse.WarehouseManager Warehouse.WarehouseManager

So following the example, the console output would simply be: 因此,按照该示例,控制台输出将简单地为:

1:Bob Wilson 1:鲍勃·威尔逊
2:Chris Warren 2:克里斯·沃伦
3:Mike Olsen 3:迈克·奥尔森
4:Aaron Erikson 4:亚伦·埃里克森

I've been trying to figure out the syntax, but LINQ is not a strength, and I'm hoping the group can help me out. 我一直在尝试弄清楚语法,但是LINQ并不是一个优势,我希望该小组可以为我提供帮助。

Seems like you need to use Enumerable.Union 似乎您需要使用Enumerable.Union

var allManagers = d.Stores.Select(m => m.StoreManager).Union(d.Stores.Select(m => m.InventoryManager))
                                                      .Union(d.Warehouses.Select(m => m.WarehouseManager));
foreach (Manager m in allManagers)
{
    Console.WriteLine("{0}:{1}", m.Id, m.Name);
}   

Note that Union returns unique values from input sequences so no need to call Distinct . 请注意, Union从输入序列返回唯一值,因此无需调用Distinct

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

相关问题 从对象列表中,将相同(或可为空)类型的多个属性放入不同的列表中 - From a list of objects, get several properties of the same (or nullable) type into a distinct list 如何选择不同的列表 <T> 来自清单 <List<T> &gt;? - How to select distinct List<T>'s from a List<List<T>>? 使用LINQ在列表列表中选择不同的属性 - Use LINQ to select distinct properties in Lists of Lists 不知道如何从 C# 中声明为抽象类类型的对象中检索属性 - Don't kwow how to retrieve properties from objects declared as abstract class type in C# Newtonsoft不会序列化具有相同值的对象,不同的对象 - Newtonsoft don't serialize objects with same values, distinct objects 如何从多个列表中选择不同且有序的数据到通用列表中? - How can I select distinct and ordered data into a generic list from multiple lists? 选择不同的名称并将对象属性组合到列表中<T> - select distinct name and combine an objects property in a List<T> Lambda 多个条件从列表到 select 对象<t></t> - Lambda multiple conditions to select objects from List<T> 如何从对象的嵌套列表中获得不同的值? - How to get distinct values from nested lists of objects? 如何使用NH生成“select count(*)as y from(select table x,y from tableName)as t”? - How to generate “select count(*)as y from (select distinct x, y from tableName) as t” using NH?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM