列表列表中字符串的唯一记录的 C# Lambda 表达式

Rak*_*esh 2 c# linq c#-4.0

我有课

public class ABCImport
{
  List<string> SegmentationList;

}
Run Code Online (Sandbox Code Playgroud)

现在我有 ABCImport 列表。

var ABCImportList=New List<ABCImport>();
Run Code Online (Sandbox Code Playgroud)

我需要来自 ABCImportList 列表中的 SegmentationList 的唯一字符串,不带空字符串。假设 ABCImportList 有 50 个 ABCImport 记录,每个 ABCImport 导入都有 SegmentationList,它可以在每个 ABCImport 中重复。所以我需要所有分段列表中的唯一字符串。

这是我到目前为止所拥有的:

ABCImportList
    .Where(
        x => x.SegmentationList
            .Where(s => !string.IsNullOrWhiteSpace(s))
    )
    .Distinct()
    .ToList()
Run Code Online (Sandbox Code Playgroud)

Fel*_*ani 5

您可以使用允许SelectMany()您指定一个集合的方法来将该集合的所有内容放入单个结果中。在您的情况下,属性值SegmentationList例如:

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