이제 특히 로컬 함수를 사용하지만 익명 형식을 만드는 대리자를 전달하여 항상 수행 할 수 있습니다.
따라서 목표가 동일한 소스에서 다른 로직을 실행하고 결과를 단일 목록으로 결합하는 것이 었습니다. 명시된 목표를 달성하기 위해 이것이 누락 된 뉘앙스가 무엇인지 확실하지 않지만 a를 반환하고 T
make에 대리자를 전달하는 T
한 함수에서 익명 유형을 반환 할 수 있습니다.
// returning an anonymous type
// look mom no casting
void LookMyChildReturnsAnAnonICanConsume()
{
// if C# had first class functions you could do
// var anonyFunc = (name:string,id:int) => new {Name=name,Id=id};
var items = new[] { new { Item1 = "hello", Item2 = 3 } };
var itemsProjection =items.Select(x => SomeLogic(x.Item1, x.Item2, (y, i) => new { Word = y, Count = i} ));
// same projection = same type
var otherSourceProjection = SomeOtherSource((y,i) => new {Word=y,Count=i});
var q =
from anony1 in itemsProjection
join anony2 in otherSourceProjection
on anony1.Word equals anony2.Word
select new {anony1.Word,Source1Count=anony1.Count,Source2Count=anony2.Count};
var togetherForever = itemsProjection.Concat(otherSourceProjection).ToList();
}
T SomeLogic<T>(string item1, int item2, Func<string,int,T> f){
return f(item1,item2);
}
IEnumerable<T> SomeOtherSource<T>(Func<string,int,T> f){
var dbValues = new []{Tuple.Create("hello",1), Tuple.Create("bye",2)};
foreach(var x in dbValues)
yield return f(x.Item1,x.Item2);
}