Returning anonymous type in C#
You can't.
You can only return object
, or container of objects, e.g. IEnumerable<object>
, IList<object>
, etc.
You can return dynamic
which will give you a runtime checked version of the anonymous type but only in .NET 4+
In C# 7 we can use tuples to accomplish this:
public List<(int SomeVariable, string AnotherVariable)> TheMethod(SomeParameter)
{
using (MyDC TheDC = new MyDC())
{
var TheQueryFromDB = (....
select new { SomeVariable = ....,
AnotherVariable = ....}
).ToList();
return TheQueryFromDB
.Select(s => (
SomeVariable = s.SomeVariable,
AnotherVariable = s.AnotherVariable))
.ToList();
}
}
You might need to install System.ValueTuple
nuget package though.