问题描述
|
假设我想遍历
string[][]
,使用匿名类型附加值,然后对结果执行通用的ForEach-Extension方法(我知道,这是个很棒的例子,但是我想您会得到简而言之!)。
这是我的代码:
//attrs = some string[][]
attrs.Select(item => new { name = HttpContext.GetGlobalResourceObject(\"Global\",item[0].Remove(0,7)),value = item[1] })
.ForEach</*????*/>(/*do stuff*/);
我到底要在ForEach的Type-Parameter中输入什么?
这是ForEach的样子:
public static void ForEach<T>(this IEnumerable<T> collection,Action<T> act)
{
IEnumerator<T> enumerator = collection.GetEnumerator();
while (enumerator.MoveNext())
act(enumerator.Current);
}
解决方法
您无需显式指定类型,因为可以从提供的参数中推断出它:
attrs.Select(item => new
{
name = HttpContext.GetGlobalResourceObject(\"Global\",item[0].Remove(0,7)),value = item[1]
})
.ForEach(x => x.name = \"something\");