2011-07-06 19 views
1

以下のコードをLINQまたはLambda式に変換します。Removeメソッドを含む入れ子のForloopとLINQを使用するIf条件

for (int indexprod = 0; indexprod < tempProduct.Count; indexprod++) 
{ 
    for (int index = 0; index < tempProduct[indexprod].Prices.Count; index++) 
    { 
    if (tempProduct[indexprod].Prices[index].Term == null || tempProduct[indexprod].Prices[index].Term == 0) 
    { 
     tempProduct[indexprod].Prices.Remove(tempProduct[indexprod].Prices[index]); 
     index--; 
    } 
    } 

    if (tempProduct[indexprod].Prices.Count == 0) 
    { 
    tempProduct.Remove(tempProduct[indexprod]); 
    indexprod--; 
    } 
} 

ASAPが必要です。

私はこれを実行しようとしました:

List<Product> tempprod1= (from p in products 
        from pr in p.Prices 
        where pr.Term == 0 
        select p).ToList<Product>(); 

ではなく、価格要素pr.Termを削除する方法を見つけ出すことができ0!。ここで

おかげで、

答えて

1

あなたが持っている:

tempProduct 
    .ForEach(p => p.Prices.RemoveAll(price => (price.Term ?? 0)==0)) 
    .RemoveAll(p => p.Prices.Count == 0); 

・ホープ、このことができますし。歓声

+0

を持っているすべての製品を削除する必要があり、機能しません。私は見ることができません。 –

0

はこの仕事とより簡潔ではないでしょう...

tempProduct.RemoveAll(p => p.Prices.Count == 0); 
+0

おかげであるが、これは、私はこれが半分の道を働いたPrice.Term == 0 –

0

Term = 0の場合、価格要素を削除する場合。以下は、クエリに応答のための

tempProduct.ForEach(p=> p.Prices.Remove(p.Prices.ToList().ToList().Where(x => x.Term == 0).FirstOrDefault())); 
関連する問題