[英]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列表:
So following the example, the console output would simply be: 因此,按照该示例,控制台输出将简单地为:
1:Bob Wilson
1:鲍勃·威尔逊
2:Chris Warren2:克里斯·沃伦
3:Mike Olsen3:迈克·奥尔森
4:Aaron Erikson4:亚伦·埃里克森
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.