अब विशेष रूप से स्थानीय कार्यों के साथ, लेकिन आप हमेशा एक प्रतिनिधि को पास करके कर सकते हैं जो गुमनाम प्रकार बनाता है।
इसलिए यदि आपका लक्ष्य एक ही स्रोतों पर विभिन्न तर्क चलाना है, और परिणामों को एक ही सूची में संयोजित करने में सक्षम है। निश्चित नहीं है कि यह निर्धारित लक्ष्य को पूरा करने के लिए क्या अनुपलब्ध है, लेकिन जब तक आप वापस लौटते हैं T
और बनाने के लिए एक प्रतिनिधि को पास करते हैं 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);
}