2016-09-08 4 views
-1

私はidnameを含むコレクションpersonsを持って参加します組織別:は2つのコレクション

[1,2]
101 - -

100 []
102 - [3]

そして最後に私はOrganizationId含むメインエンティティ有する:

entity.OrganizationId = 100; 

私はworkersを通過し、すべての人のIDを取得する必要があります(1,2)、最後にその人の名前をpersonsから取得する必要があります。どうやってやるの?

+0

それはあなたが何を尋ねているのかが不明です –

+0

何が問題なのですか?それはlinq joinの簡単な使用です –

答えて

1

あなたはこの特定のOrganisationIdから人々をしたい場合:

var personsFromOrganisation = workers[OrganizationId].Select(i => persons[i]); 

あなたが直接(辞書を使用して、ここ)の者を対応するすべてのOrganisationIdを関連付けるオブジェクトをしたい場合:

var organisationsIds = entities.Select(e => e.OrganisationId).Distinct(); 
var personsByOrganisation = organisationsIds.ToDictionary(id => id, id => workers[id].Select(pid => persons[pid])); 
1

あなたがサブクエリを使用して必要なのIdsを取得し、そのことで参加することができます

var personsByOrganization = 
     from p in persons 
     join id in workers.Where(x => x.Key == entity.OrganizationId) 
          .SelectMany(x => x.Value) 
     on p.Key equals id 
     select p; 

無参加アプローチ:

HashSet<int> Ids = new HashSet<int>(workers.Where(x => x.Key == entity.OrganizationId) 
              .SelectMany(x => x.Value)); 

Dictionary<int, string> personsByOrganization = 
     persons.Where(x => Ids.Contains(x.Key)) 
       .ToDictionary(x => x.Key, x => x.Value); 
+0

ありがとう、どうすれば 'join'を使わずにそれを得ることができますか? –

1

IDのを取得するためにLINQは必要ない:

IEnumerable<int> orgWorkers = null; 
bool containsOrg = workers.TryGetValue(entity.OrganizationId, out orgWorkers); 

今では名を取得するのは簡単です:

List<string> workerNames = new List<string>(); 
if(containsOrg) 
{ 
    workerNames = orgWorkers 
     .Where(id => persons.ContainsKey(id)) 
     .Select(id => persons[id]) 
     .ToList(); 
} 
関連する問題