如何将IDS集合“爆炸”成一个DISTINCT IDS集合?

时间:2012-07-06 10:20:55

标签: c# .net linq

假设我有这个LINQ查询(在.NET 4.0上):

IEnumerable<Service> listService = (from MyObject myObj in new MyObjects()
                                        select myObj.Services);

如您所见,listService是“集合”的集合(Services是“服务”的集合,其中包含标题,ID(我需要的)以及其他一些字段)。

我想在LINQ中做的是使用该查询获得IEnumerable<int>,每个Service的每个Services都有不同的ID列表。

有没有办法在LINQ上执行此操作,或者我需要与一些foreach循环并使用另一个数组进行管理?

示例:

my first myObj (so, MyObjects[0]) have got a collection, called Service, which contain single Service. Every Service have got a single id, respectively : "1", "2", "4"

my second myObj (so, MyObjects[1]) have got a collection, called Service, which contain single Service. Every Service have got a single id, respectively : "4", "5", "1", "2"}

我需要的是一个“单一”集合,其中包含来自每个myObj的每个Service集合的ID列表。此列表必须具有不同的值。

3 个答案:

答案 0 :(得分:4)

编辑:更新假设初始查询应真正读取如下内容:

IEnumerable<IEnumerable<int>> listService = ...;

假设你只是试图压扁结果,听起来你可能只是想要:

var results = listService.SelectMany(service => service)
                         .Distinct();

(如果那不是你想要的,请澄清一下这个问题 - 目前这个问题相当令人困惑。)

答案 1 :(得分:1)

在查询语法中:

IEnumerable<int> listService = (
       from MyObject myObj in new MyObjects()
       from id in myObj.Services).Distinct();

答案 2 :(得分:1)

由于您没有显示课程内容,因此我从您的描述中实现了我可以猜到的内容。这与您的对象匹配吗?

class Service
{
    public int ID { get; set; }
    public string Name { get; set; }
}

class MyObject
{
    public IEnumerable<Service> Services { get; set; }
}

class MyObjects : List<MyObject>
{

}

如果是这样,那么如果我在MyObjects的构造函数中粘贴以下内容:

class MyObjects : List<MyObject>
{
    public MyObjects()
    {
        Add(new MyObject 
            { 
                Services = new List<Service>()
                    {
                        new Service { ID = 1, Name = "foo" },
                        new Service { ID = 2, Name = "bar" },
                    }
            });
        Add(new MyObject
        {
            Services = new List<Service>()
                    {
                        new Service { ID = 3, Name = "baz" },
                        new Service { ID = 4, Name = "foo1" },
                        new Service { ID = 1, Name = "dup 1"}
                    }
        });
    }
}

我可以像这样得到不同的ID:

var distinctIDs = (from myObj in new MyObjects()
               from service in myObj.Services
               select service.ID).Distinct();

以下测试程序:

static void Main(string[] args)
{
    var objects = new MyObjects();
    var distinctIDs = (from myObj in new MyObjects()
                       from service in myObj.Services
                       select service.ID).Distinct();
    var notDistinctIDs = from myObj in new MyObjects()
                         from service in myObj.Services
                         select service.ID;
    foreach (var id in distinctIDs)
    {
        Console.WriteLine("Distinct ID: {0}", id);
    }
    Console.WriteLine("---");
    foreach (var id in notDistinctIDs)
    {
        Console.WriteLine("Not Distinct ID: {0}", id);
    }
}

打印:

Distinct ID: 1
Distinct ID: 2
Distinct ID: 3
Distinct ID: 4
---
Not Distinct ID: 1
Not Distinct ID: 2
Not Distinct ID: 3
Not Distinct ID: 4
Not Distinct ID: 1