다음 (무의미하지만, 그림 목적의) 테스트 클래스 :동적 LINQ와 선택()
public class Test
{
public IEnumerable<string> ToEnumerableStrsWontCompile(IEnumerable<dynamic> t)
{
return t.Select(x => ToStr(x));
}
public IEnumerable<string> ToEnumerableStrsWillCompile(IEnumerable<dynamic> t)
{
var res = new List<string>();
foreach (var d in t)
{
res.Add(ToStr(d));
}
return res;
}
public string ToStr(dynamic d)
{
return new string(d.GetType());
}
}
는 왜 t.Select(x => ToStr(x))
에 다음과 같은 오류와 함께 컴파일되지 않습니다?
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<dynamic>'
to 'System.Collections.Generic.IEnumerable<string>'. An explicit conversion
exists (are you missing a cast?)
두 번째 방법에는 오류가 없습니다.
+1 설명과 특히 방법 그룹 버전 –