Using C# 7.0 we still can't return anonymous types but we have a support of tuple types and thus we can return a collection of tuple
(System.ValueTuple<T1,T2>
in this case). Currently Tuple types
are not supported in expression trees and you need to load data into memory.
The shortest version of the code you want may look like this:
public IEnumerable<(int SomeVariable, object AnotherVariable)> TheMethod()
{
...
return (from data in TheDC.Data
select new { data.SomeInt, data.SomeObject }).ToList()
.Select(data => (SomeVariable: data.SomeInt, AnotherVariable: data.SomeObject))
}
Or using the fluent Linq syntax:
return TheDC.Data
.Select(data => new {SomeVariable: data.SomeInt, AnotherVariable: data.SomeObject})
.ToList();
.Select(data => (SomeVariable: data.SomeInt, AnotherVariable: data.SomeObject))
Using C# 7.1 we can omit properties names of tuple and they will be inferred from tuple initialization like it works with anonymous types:
select (data.SomeInt, data.SomeObject)
// or
Select(data => (data.SomeInt, data.SomeObject))