2016-04-19 3 views
0

私は個々のデータを持っている5つの変数を持っています。それから、それぞれの課金ボリュームを1つの変数に連結しています。これは正常に動作します。連想リストのヌルチェック

ここでは、チャージボリュームが他の変数のためnullであるため、データが1つしかない(var CCPcvにデータがあります)残りのデータにはデータがないと仮定して、var CombinedPcv改行が行われます。 n個のシナリオがあります(2つはデータ、3つはデータなど)。

1つのアプローチでは、個々の変数をNULL値としてチェックしてから、変数の組み合わせに対してヌルチェックを行います。

より良いアプローチを提案してください。ここで

var CCPcv = (GuidedPcvYear1CCViewModel)wizard.Steps[est bettgwizard.OrderedSteps[typeof(GuidedPcvYear1CCViewModel)]]; 
var CPCPcv = (GuidedPcvYear1CPCViewModel)wizard.Steps[wizard.OrderedSteps[typeof(GuidedPcvYear1CPCViewModel)]]; 
var VpayPcv = (GuidedPcvYear1VPAViewModel)wizard.Steps[wizard.OrderedSteps[typeof(GuidedPcvYear1VPAViewModel)]]; 
var BIPPcv = (GuidedPcvYear1BIPViewModel)wizard.Steps[wizard.OrderedSteps[typeof(GuidedPcvYear1BIPViewModel)]]; 
var GnicsCCPcv = (GuidedPcvYear1CCGnicsViewModel)wizard.Steps[wizard.OrderedSteps[typeof(GuidedPcvYear1CCGnicsViewModel)]]; 

var CombinedCV = CCPcv.ChargeVolumes.ConvertAll(cv=>cv).Concat(CPCPcv.ChargeVolumes.ConvertAll(x=>x).Concat(VpayPcv.ChargeVolumes.ConvertAll(z=>z).Concat(BIPPcv.ChargeVolumes.ConvertAll(a=>a).Concat(GnicsCCPcv.ChargeVolumes.ConvertAll(s=>s))))); 
year1ChargeVolumes = CombinedCV.ToList(); 
+2

あなたはヌルチェックと* Concat *を行い、すべての変数/コレクションにそれを再利用する独自の拡張メソッド*を書くことができます – bit

+0

上記の例を挙げてもらえますか? – user662285

+1

'ConvertAll'とネストされた' Concat'sはどういったものですか?あなたの列挙型が決してヌルではなく、次々と連結していることを確かめてください。 – Luaan

答えて

0

あなたがそうのようなヘルパーメソッドを書くことができ、次のよう

public static IEnumerable<T> ConcatTreatingNullsAsEmpty<T>(params IEnumerable<T>[] sequences) 
{ 
    return 
     from sequence in sequences where sequence != null 
     from item in sequence 
     select item; 
} 

そして、それを使用する:

IEnumerable<int> seq1 = Enumerable.Range(1, 5); 
IEnumerable<int> seq2 = null; 
IEnumerable<int> seq3 = Enumerable.Range(6, 5); 
IEnumerable<int> seq4 = null; 
IEnumerable<int> seq5 = Enumerable.Range(11, 5); 

var numbers = ConcatTreatingNullsAsEmpty(seq1, seq2, seq3, seq4, seq5); 

Console.WriteLine(string.Join(", ", numbers)); // Prints 1, 2, ..., 14, 15 

を私はあなたのコードは次のようになり思う:

var CombinedCV = ConcatTreatingNullsAsEmpty(
    CCPcv  .ChargeVolumes, 
    CPCPcv  .ChargeVolumes, 
    VpayPcv .ChargeVolumes, 
    BIPPcv  .ChargeVolumes, 
    GnicsCCPcv .ChargeVolumes); 
0

は、拡張メソッドの例です。

static class MyConcatExtension 
{ 
    public static IEnumerable<T> ConcatNotNulls<T>(this IEnumerable<T> collection1, IEnumerable<T> collection2) 
    { 
     // collection is null, you might want to check collection1 as well 
     if (collection2 == null) 
     { 
      return collection1; 
     } 

     // collection of reference types has null items 
     if (!typeof(T).IsValueType) 
     { 
      return collection1.Concat(collection2.Where(item => item != null)); 

     } 

     // collection of value types 
     return collection1.Concat(collection2); 
    } 
} 

そして、あなたが好きそれを使用することができます:

list1.ConcatNotNulls(list2); 
0

さて、1つのアプローチは、あなたがnull enumerablesを持ったことがないことを確認することです - 空の列挙が必要な場合は、nullの代わりにEnumerable.Emptyを使用してください。

これが実行可能でない場合は、空の列挙に列挙nullを変換するための単純な拡張メソッドを行うことができます。

public static IEnumerable<T> NotNull<T>(this T @this) 
    => Enumerable.Empty<T>(); 

また、巣Concat sの必要はありません。

var combined = 
    CCPcv?.ChargeVolumes?.NotNull() 
    .Concat(CPCPcv?.ChargeVolumes?.NotNull()) 
    .Concat(VpayPcv?.ChargeVolumes?.NotNull()) 
    .Concat(BIPPcv?.ChargeVolumes?.NotNull()) 
    .Concat(GnicsCCPcv?.ChargeVolumes?.NotNull()); 

最後に、オブジェクトが実際に共通のインターフェースを共有するように思えます。あなただけに追加する前にそれらを変換する必要が

 var tempList = new List<GuidedPcvYear1CCViewModel>() { CCPcv ,CPCPcv,VpayPcv,BIPPcv,GnicsCCPcv}; 
     var CombinedCv = tempList.Where(i => i != null).ToList(); 

を次のように

public static IEnumerable<ICV> DoStuff(params ICV[] cvs) 
    => cvs.SelectMany(i => i?.ChargeVolumes?.NotNull()); 
0

は、あなたがそれを行うことができます。その場合は、あなたは複数のenumerablesに同じ操作を適用する別のヘルパーメソッドを書くことができますリスト。