ขณะนี้มีฟังก์ชั่นเฉพาะที่โดยเฉพาะ แต่คุณสามารถทำได้โดยส่งผ่านตัวแทนที่ทำให้เป็นประเภทที่ไม่ระบุตัวตน
ดังนั้นหากเป้าหมายของคุณคือเรียกใช้ตรรกะที่แตกต่างกันในแหล่งข้อมูลเดียวกันและสามารถรวมผลลัพธ์ไว้ในรายการเดียวได้ ไม่แน่ใจว่าสิ่งที่ขาดหายไปเล็กน้อยเพื่อให้บรรลุเป้าหมายที่ระบุไว้ แต่ตราบใดที่คุณส่งคืนT
และส่งมอบผู้รับมอบสิทธิ์ให้T
คุณสามารถส่งคืนประเภทที่ไม่ระบุตัวตนจากฟังก์ชันได้
void LookMyChildReturnsAnAnonICanConsume()
{
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} ));
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);
}