0

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

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

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

我想在 LINQ 中做的是通过该查询获得一个, 以及每个IEnumerable<int>的不同 ID 列表。ServiceServices

有没有办法在 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 的每个服务集合的 ID 列表。此列表必须具有 Distinct 值。

4

3 回答 3

4

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

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

假设您只是想使结果变平,听起来您可能只想:

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

(如果这不是您所追求的,请澄清这个问题 - 目前相当混乱。)

于 2012-07-06T10:22:16.430 回答
1

在查询语法中:

IEnumerable<int> listService = (
       from MyObject myObj in new MyObjects()
       from id in myObj.Services).Distinct();
于 2012-07-06T10:25:54.880 回答
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
于 2012-07-06T10:56:28.237 回答