frenchie frenchie - 1 year ago 130
C# Question

Returning anonymous type in C#

I have a query that returns an anonymous type and the query is in a method. How do you write this:

public "TheAnonymousType" TheMethod(SomeParameter)
using (MyDC TheDC = new MyDC())
var TheQueryFromDB = (....
select new { SomeVariable = ....,
AnotherVariable = ....}

return "TheAnonymousType";

Answer Source

You can't.

You can only return object, or container of objects, e.g. IEnumerable<object>, IList<object>, etc.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download