列表列表中字符串的唯一记录的 C# Lambda 表达式
C# Lambda Expression for Unique Record for string in list of List
我有Class
public class ABCImport
{
List<string> SegmentationList;
}
现在我有了 ABCImport 列表。
var ABCImportList=New List<ABCImport>();
我需要来自 ABCImportList 列表中的 SegmentationList 的唯一字符串,不带空字符串。假设 ABCImportList 有 50 条 ABCImport 记录,每个 ABCImport 导入都有 SegmentationList,它可以在每个 ABCImport 中重复。所以我需要所有细分列表中的唯一字符串。
这是我目前拥有的:
ABCImportList
.Where(
x => x.SegmentationList
.Where(s => !string.IsNullOrWhiteSpace(s))
)
.Distinct()
.ToList()
您可以使用 SelectMany()
方法,该方法允许您指定一个集合,将所有该集合整合到一个结果中。在您的例子中,SegmentationList
属性 的值,例如:
var segmentationList = ABCImportList.SelectMany(x => x.SegmentationList.Where(s => !string.IsNullOrEmpty(s)
&& !string.IsNullOrWhiteSpace(s))
.Distinct()
.ToList();
我有Class
public class ABCImport
{
List<string> SegmentationList;
}
现在我有了 ABCImport 列表。
var ABCImportList=New List<ABCImport>();
我需要来自 ABCImportList 列表中的 SegmentationList 的唯一字符串,不带空字符串。假设 ABCImportList 有 50 条 ABCImport 记录,每个 ABCImport 导入都有 SegmentationList,它可以在每个 ABCImport 中重复。所以我需要所有细分列表中的唯一字符串。
这是我目前拥有的:
ABCImportList
.Where(
x => x.SegmentationList
.Where(s => !string.IsNullOrWhiteSpace(s))
)
.Distinct()
.ToList()
您可以使用 SelectMany()
方法,该方法允许您指定一个集合,将所有该集合整合到一个结果中。在您的例子中,SegmentationList
属性 的值,例如:
var segmentationList = ABCImportList.SelectMany(x => x.SegmentationList.Where(s => !string.IsNullOrEmpty(s)
&& !string.IsNullOrWhiteSpace(s))
.Distinct()
.ToList();