C# SelectMany怎么了?
我遇到了SelectMany表达式的一个问题,我就是想不起来 考虑一下:我有一个此类对象的集合C# SelectMany怎么了?,c#,linq,C#,Linq,我遇到了SelectMany表达式的一个问题,我就是想不起来 考虑一下:我有一个此类对象的集合 class Tag { string DisplayText { get; set; } string Key { get; set; } int Value { get; set; } } 现在,我试图让我所有的显示文本实际上成为更复杂表达式的一部分: var texts = AvailableTags.SelectMany(t => t.DisplayText);
class Tag
{
string DisplayText { get; set; }
string Key { get; set; }
int Value { get; set; }
}
现在,我试图让我所有的显示文本实际上成为更复杂表达式的一部分:
var texts = AvailableTags.SelectMany(t => t.DisplayText);
现在为什么这会返回一个IEnumerable而不是IEnumerable???我遗漏了什么吗?如果AvailableTags是一个IEnumerable列表,那么您只需使用
var texts = AvailableTags.Select(t => t.DisplayText);
使用SelectMany得到的奇怪结果正如@derloopkat所说的那样,是因为字符串是字符的集合。
因此,您可以想象您的代码如下:
class Tag
{
List<char> DisplayText { get; set; }
string Key { get; set; }
int Value { get; set; }
}
当您使用SelectMany时,您将获得每个DisplayText中包含的所有字符,然后结果将变平。如果AvailableTags是一个IEnumerable列表,那么您只需使用
var texts = AvailableTags.Select(t => t.DisplayText);
使用SelectMany得到的奇怪结果正如@derloopkat所说的那样,是因为字符串是字符的集合。
因此,您可以想象您的代码如下:
class Tag
{
List<char> DisplayText { get; set; }
string Key { get; set; }
int Value { get; set; }
}
当您使用SelectMany时,您将获得每个DisplayText中包含的所有字符,然后结果将变平。它返回IEnumerable,因为字符串是字符的集合。请查看:。特别是,搜索列表“Fruits”在其中一个答案中包含“apple”,返回IEnumerable,因为字符串是查找的字符的集合:。特别是,搜索“水果”列表时,其中一个答案中包含“苹果”