2015-04-20 50 views
2

的列表字符串我有類C#Lambda表達式爲唯一的記錄在列表

public class ABCImport 
{ 
    List<string> SegmentationList; 

} 

現在我有ABCImport的名單。

var ABCImportList=New List<ABCImport>(); 

我需要從SegmentationList從ABCImportList列表中唯一的字符串沒有空字符串.lets說ABCImportList有ABCImport 50記錄,每一ABCImport進口有SegmentationList可在每個ABCImport。所以是重複的,我需要的唯一字符串從所有分割名單。

這是我到目前爲止有:

ABCImportList 
    .Where(
     x => x.SegmentationList 
      .Where(s => !string.IsNullOrWhiteSpace(s)) 
    ) 
    .Distinct() 
    .ToList() 
+0

沒有什麼?你有一個'ABCImports'的集合 - 你想要一個扁平的分割列表嗎? (聽起來像'ABCImportList.SelectMany(x => x.SegmentationList).Distinct()'會這樣做... –

+0

我想獲得唯一的字符串像bleow ABCImportList.Where(x => x.SegmentationList.Where(s =>!string.IsNullOrWhiteSpace(s)))。Distinct()。ToList() – Rakesh

回答

4

可以使用SelectMany()方法,它允許你指定一個集合來得到這一切集合到一個結果。在你的情況下,SegmentationList屬性的值,對於樣本:

var segmentationList = ABCImportList.SelectMany(x => x.SegmentationList.Where(s => !string.IsNullOrEmpty(s) 
                       && !string.IsNullOrWhiteSpace(s)) 
            .Distinct() 
            .ToList();