2016-04-13 3 views
1

C#の新機能です。問題は、私が配列に対して "allowedA"と "allowedB"配列を使って、私のapi呼び出しの結果をフィルタリングする必要があるということです。ループに対してチェックするラムダ式を編集する方法がわかりません。ラムダ式を使用してC#を使用して値をフィルタリングして加算する

var activities = await _restClientTaxonomy.GetTaxonomyFullAsync(TAXONOMY_CLASSIFICATIONID_FOR_ACTIVITY); 
var activityTypes = await _restClientTaxonomy.GetTaxonomyFullAsync(TAXONOMY_CLASSIFICATIONID_FOR_ACTIVITY_TYPES); 

      var documentEventxx = activities.Select(type => type.Id); 

      long [] allowedA = new long []{ 7137, 40385637}; 
      long [] allowedB = new long []{ 7137, 40385637}; 


      foreach (long value in documentEventxx) 
      { 

       foreach (var item in allowed) 
       { 

        if (item == value) { 
         //These are the values I am looking for -> values that are part of the documentEventxx and allowedB. 
        } 
       } 
      } 

      var result = activityTypes.Select(type => new CategoryViewModel 
      { 
       Id = type.Id,//This is where I want to add only items that are in the allowedA array 
       Text = type.Name, 
       Types = activities.Where(a => a.ParentId == type.Id).Select(t => new TaxonomyMemberTextItem 
       { 
        Id = t.Id, //This is where I want to add only items that are in the allowedB array 
        Text = t.Name 
       }).ToList() 
      }).ToArray(); 

ラムダ式とforeachループについて読んでいますので、ランダムリンクを投稿しないでください。

ありがとうございます。

+0

アクティビティを試しましたか(a => a.ParentId == type.Id && allowedB.Contains(a.Id))? .Contains()をIndexOf(a.id)!= -1で変更する必要があるかもしれませんが、試してみてください。 –

答えて

0

フィルターには.Whereを使用してください。 .Select新しいタイプのリストを作成します。だから、フィルタリングするためには、あなたがしたいオブジェクトのリストを作成します。

var result = activityTypes.Where(type=>isAllowed(type.Id)).Select(type => new CategoryViewModel 
{ 
    Id = type.Id,//This is where I want to add only items that are in the allowedA array 
    Text = type.Name, 
    Types = activities.Where(a => a.ParentId == type.Id&&isAllowed(a.Id)).Select(t => new TaxonomyMemberTextItem 
    { 
     Id = t.Id, //This is where I want to add only items that are in the allowedB array 
     Text = t.Name 
    }).ToList() 
}).ToArray(); 
+0

ところで、TaxonomyMemberTextItemを選択すると、 't'というラムダ変数を使用しますが、読みやすいようにWhereのようにソースの型を表す必要がありますので、' a'を試してみてください。 –

1

は、選択する前に値をフィルタリングします。

  activityTypes.Where(x=>allowedA.Contains(x.Id)).Select(type => new CategoryViewModel 
      { 
       Id = type.Id, 
       Text = type.Name, 
       Types = activities.Where(a => a.ParentId == type.Id && allowedB.Contains(a.Id)).Select(t => new TaxonomyMemberTextItem 
       { 
        Id = t.Id, 
        Text = t.Name 
       }).ToList() 
      }) 
関連する問題