Returning anonymous type in C#

frenchie picture frenchie · Apr 9, 2012 · Viewed 110.3k times · Source

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 = ....}
                           ).ToList();

      return "TheAnonymousType";
    }
}

Answer

abatishchev picture abatishchev · Apr 9, 2012

You can't.

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