Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return anonymous type from c# method that uses LINQ to SQL [duplicate]

People also ask

How do I return an anonymous function in C#?

An anonymous method can return a value. The value is returned by use of the return statement. The type of the return value must be compatible with the return type specified by the delegate. In this version, the value of sum is returned by the code block that is associated with the count delegate instance.

What is an anonymous type in C?

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.

How do you define anonymous type?

Essentially an anonymous type is a reference type and can be defined using the var keyword. You can have one or more properties in an anonymous type but all of them are read-only. In contrast to a C# class, an anonymous type cannot have a field or a method — it can only have properties.


Make the anonymous type into a class...

public class Person
{
    public Person() {
    }

    public String Name { get; set; }
    public DateTime DOB { get; set; }
}


Person p = 
    from person in db.People 
    where person.Id = 1 
    select new Person { 
        Name = person.Name,
        DOB = person.DateOfBirth
    }

You cannot type any method in C# to be the explicit type of an anonymous types. They cannot be "named" so to speak and hence cannot appear in metadata signatures.

If you really want to return a value which is an anonymous type there are 2 options

  1. Have the return type of the method be System.Object. You can then do evil casting hacks to get a typed value in another method. This is very fragile and I don't recommend it.
  2. Use a generic method and a type inference trick to get the return type correct. This would require a very interesting signature definition for your approach.

Anonymous types were not really meant to be passed around in this fashion. At the point you need to pass them around between your functions in this manner, you're better off explicitly defining a type.


Using var doesn't make it an anonymous type. Using var just means let this variable be of the type available on the right-hand side of the assignment. It's just short hand. If the thing on the right-hand side is a real class, the variable will be of that type.

For example:

var person = new Person { Name = "bob" };

The person variable is of type Person, even though it used the var keyword.

Anonymous types are created using new { Name = ... }. In this case it's a new, anonymous class. The only thing you can assign it to is a variable defined using var (or object) since there is no existing name to use.

For example:

var person = new { Name = "bob" };

In this case, person is an anonymous type defined at run time.

Generally, as @Chalkey says, if you want to pass the result back to another method, use a named type, not an anonymous one.

If you are forced to use an anonymous type, you'll have to pass it back as an object of type Object, then use reflection to get at it's properties.


Jon Skeet wrote a blog on how to do this, which is quite rightly titled Horribly Grotty Hack. Just as the title suggests, you really shouldn't be looking for ways to return an anonymous type. Instead, you should be creating a type that can be returned as this is the correct way to implement this feature.

Therefore, I recommend you create a concrete definition of the type to be returned and then populate that in your query for it to be returned.


It kind of depends on how the calling code is going to use the data.

If you are doing simple data binding, are really don't care about the type (i.e. you don't have to explicitly access properties in your C# code), you can pass the results back as an IEnumberable.

In most databinding cases you are calling properties by name, via magic strings, anyway, so the exact type doesn't matter anyway.

Otherwise, you need to convert the anonymous type to a named type.