关于LINQ语法只是一点点关注。我用IEnumerable<IEnumerable<T>>
压扁了SelectMany(x => x)
。
我的问题是lambda表达式x => x
。它看起来有点难看。是否有一些静态'身份功能'对象,我可以使用而不是x => x
?像SelectMany(IdentityFunction)
这样的东西?
注意:这个答案对于C#3是正确的,但在某些时候(C#4?C#5?)类型推断得到了改进,因此可以很容易地使用下面显示的IdentityFunction
方法。
不,没有。它必须是通用的,首先是:
public static Func<T, T> IdentityFunction<T>()
{
return x => x;
}
但是类型推断不起作用,所以你必须这样做:
SelectMany(Helpers.IdentityFunction<Foo>())
这比x => x
更加丑陋。
另一种可能性是你在扩展方法中包装它:
public static IEnumerable<T> Flatten<T>
(this IEnumerable<IEnumerable<T>> source)
{
return source.SelectMany(x => x);
}
不幸的是,对于通用方差,它可能会在C#3中的各种情况下犯规......例如,它不适用于List<List<string>>
。你可以使它更通用:
public static IEnumerable<TElement> Flatten<TElement, TWrapper>
(this IEnumerable<TWrapper> source) where TWrapper : IEnumerable<TElement>
{
return source.SelectMany(x => x);
}
但是,我再次提到了类型推断问题,我怀疑......
编辑:回应评论......是的,C#4使这更容易。或者更确切地说,它使第一个Flatten
方法比在C#3中更有用。这是一个在C#4中工作的示例,但在C#3中不起作用,因为编译器无法从List<List<string>>
转换为IEnumerable<IEnumerable<string>>
:
using System;
using System.Collections.Generic;
using System.Linq;
public static class Extensions
{
public static IEnumerable<T> Flatten<T>
(this IEnumerable<IEnumerable<T>> source)
{
return source.SelectMany(x => x);
}
}
class Test
{
static void Main()
{
List<List<string>> strings = new List<List<string>>
{
new List<string> { "x", "y", "z" },
new List<string> { "0", "1", "2" }
};
foreach (string x in strings.Flatten())
{
Console.WriteLine(x);
}
}
}
除非我误解了这个问题,否则以下内容在C#4中似乎对我有用:
public static class Defines
{
public static T Identity<T>(T pValue)
{
return pValue;
}
...
然后,您可以在示例中执行以下操作:
var result =
enumerableOfEnumerables
.SelectMany(Defines.Identity);
除了在任何地方使用Defines.Identity
,你还可以使用看起来像x => x
的lambda。
使用C#6.0并且如果您引用FSharp.Core,您可以:
using static Microsoft.FSharp.Core.Operators
然后你可以自由地做:
SelectMany(Identity)
这是否按照您想要的方式工作?我意识到Jon发布了这个解决方案的一个版本,但他有第二个类型参数,只有在生成的序列类型与源序列类型不同时才需要。
public static IEnumerable<T> Flatten<T>(this IEnumerable<T> source)
where T : IEnumerable<T>
{
return source.SelectMany(item => item);
}
你可以接近你需要的东西。考虑IEnumerable<T>
的扩展方法,而不是常规静态函数,就像标识函数是集合一样,而不是类型(集合可以生成其项的标识函数):
public static Func<T, T> IdentityFunction<T>(this IEnumerable<T> enumerable)
{
return x => x;
}
有了这个,你不必再次指定类型,并写:
IEnumerable<IEnumerable<T>> deepList = ... ;
var flat = deepList.SelectMany(deepList.IdentityFunction());
虽然这确实有点侮辱,我可能会选择x=>x
。此外,你不能流利地使用它(链接),所以它并不总是有用。
使用C#6.0,情况正在好转。我们可以按@Sahuagin建议的方式定义Identity函数:
static class Functions
{
public static T It<T>(T item) => item;
}
然后在SelectMany
using static
构造函数中使用它:
using Functions;
...
var result = enumerableOfEnumerables.SelectMany(It);
我认为这种方式看起来非常简洁。我还发现在构建字典时Identity功能很有用:
class P
{
P(int id, string name) // sad, we are not getting Primary Constructors in C# 6.0
{
ID = id;
Name = id;
}
int ID { get; }
int Name { get; }
static void Main(string[] args)
{
var items = new[] { new P(1, "Jack"), new P(2, "Jill"), new P(3, "Peter") };
var dict = items.ToDictionary(x => x.ID, It);
}
}
我将使用一个带有单个静态属性的简单类,并添加尽可能多的行
internal class IdentityFunction<TSource>
{
public static Func<TSource, TSource> Instance
{
get { return x => x; }
}
}
SelectMany(IdentityFunction<Foo>.Instance)