Especially with local functions, but you can always do this by passing in a delegate that creates an anonymous type.
So if your goal was to run another logic in the same sources and be able to combine the results in one list. I'm not sure what the nuance of this is not enough to achieve the goal, but as long as you return T and pass the delegate to create T , you can return an anonymous type from the function.
// 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); }
Maslow Feb 11 '19 at 14:40 2019-02-11 14:40
source share