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のリストを含む「単一の」コレクションです。このリストは、個別の値である必要があります。

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 に答える