views:

152

answers:

8

Do any one know how to return an anonymous type. I am using Linq where i need to return the following code

private <What's the return type to be provided here> SampleLinq(Int32 Num)
    {
        var query = (from dept in obj.DeptTable where dept.Id == Num select new { dept.DeptName, dept.DeptId });
        return (query)       

    }
A: 

You can't return an Anonymous Type from a method.

You can create a simple Class to wrap the Anonymous Type, but you still need a Class (or cast to object).

Keep in mind, though, that if you cast to object there's no way to cast back. You'll need reflection to read any data.

Justin Niessner
+1  A: 

Sorry to say but you cannot return anonymous type out side the scope of method.

This is the alternate way to get anonmous type

// Method that returns anonymous type as object
object ReturnAnonymous()
{
  return new { City="Prague", Name="Tomas" };
}

// Application entry-point
void Main()
{
  // Get instance of anonymous type with 'City' and 'Name' properties
  object o = ReturnAnonymous();

  // This call to 'Cast' method converts first parameter (object) to the
  // same type as the type of second parameter - which is in this case 
  // anonymous type with 'City' and 'Name' properties
  var typed = Cast(o, new { City="", Name="" });
  Console.WriteLine("Name={0}, City={1}", typed.Name, typed.City);
}

// Cast method - thanks to type inference when calling methods it 
// is possible to cast object to type without knowing the type name
T Cast<T>(object obj, T type)
{
  return (T)obj;
}

you can use it only for types in one assembly (two anonymous types from two different assemblies will be internally compiled to two different types that can't be converted using this trick).

Pranay Rana
A: 

you can't do that. that is why it is called anonymous. It doesn't have a name. But you always can cast it to object

Andrey
A: 
private object SampleLinq(Int32 Num)
{
return (from dept in obj.DeptTable where dept.Id == Num select new { dept.DeptName, dept.DeptId });
}
David Radcliffe
but how to re-cast the object to var to get the properties as object does not contain public definition for getenumerator?
Without additional direction such as FirstOrDefault(), the query is going to return an IEnumerable<>. And then you have to use Reflection to figure out how to use it.
GalacticCowboy
@renjithmaxy - "var" != "<my anonymous type>". A *var* is still strongly typed at compile time.
GalacticCowboy
+3  A: 

Well, you can't actually do that, but here's a hack on this.

this. __curious_geek
A: 

It depends what you looking to do with the return vale. If your going to bind it in the UI you can just rerun IEnumerable/IQueryable, if your going to use reflection just return type object, if your using c# 4.0 you can return a dynamic type, or if your using EF or Linq to SQL you can make a concrete class instead and use the concrete placeholder technique. If you want more details on the last technique I can give some assistance.

jpierson
A: 

Return Dynamic type:

      public static dynamic  getCustomer() 
{ 
    ..... 
    var x = from c in customers 
            select new {Fname = c.FirstName}; 

    return x; 
} 

static void Main(string[] args) 
{ 
    dynamic x = getCustomer(); 
    Console.WriteLine(Enumerable.First(x).Fname); 
    Console.ReadKey(); 
} 
Stacker
A: 

The answers you see from the hack is a lot of work just to get an anonymous type through a method boundary. You shouldn't be doing this. If you need to pass something back from a method, you should be passing concrete types.

Matthew Abbott