Is there a way to return Anonymous Type from method?
Returning it as a System.Object
is the only way to return an anonymous type from a method. Unfortunately there is no other way to do this since anonymous types were designed specifically to prevent their use in this way.
There are some tricks that you can do to in conjunction with returning an Object
that allow you to get close. If you are interested in this workaround please read Can't return anonymous type from method? Really?.
Disclaimer: Even though the article I linked does show a workaround that doesn't mean it is a good idea to do it. I would strongly discourage you using this approach when creating a regular type would be safer and easier to understand.
Returning anonymous type in C#
You can't.
You can only return object
, or container of objects, e.g. IEnumerable<object>
, IList<object>
, etc.
Return Anonymous Type from a function
This is a very popular question. In general you cannot return an anonymous type due to the requirement of strong typing. However there are a couple of workarounds.
- Create a simple type to represent the return value. (See here and here). Make it simple by generating from usage.
- Create a helper method to cast to the anonymous type using a sample instance for casting.
Method that returns anonymous type C#
You shouldn't return an anonymous type. Instead you should create a regular named class and use that as return type.
Anonymous types are good when their scope is just inside the method itself. They have no use outside the method. How would you for example know the property names if their definition is not public? The only solution is to create a class.
If you still want to return an anonymous type, you should return object
or dynamic
, both I am not really happy about when using that as return type.
Regarding your update: you have to use the named types in the code initializing the instances too (probably LINQ). C# doesn't automatically convert anonymous types to named types.
Is it good practice to return anonymous type in respond to HTTP GET request?
You right, you will have the same resulting serialized object (xml, json), and you can use anonymous type. But you should keep in mind:
- When your explicitly define resulting class, your code will be
cleaner - For explicitly defined resulting class your may define some validation
rules using attributes for serializer - If you use documentation tools, for example swagger, you also may use attributes to provide additional documentation.
Api should not be ambiguous.
Everything depends on your preferences. If you want use anonymous type, you may.
How to return anonymous type from EF sql query
It appears that EF doesn't support returning any types other than ones that have EF mappings when using the ObjectContext.ExecuteStoreQuery() method.
I ended up using a SqlCommand object and datareader with an anonymous type.
How to return anonymous type from c# method that uses LINQ to SQL
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
}
Related Topics
How to Unload an Assembly from the Primary Appdomain
How to Hide a Column (Gridview) But Still Access Its Value
Recursive Hierarchy - Recursive Query Using Linq
Convert Array of Bytes to Bitmapimage
How to Render a Razor View to a String in ASP.NET MVC 3
Loading Multiple Versions of the Same Assembly
How to Convert Ticks to a Date Format
Configure JSON.Net to Ignore Datacontract/Datamember Attributes
Using the Null-Conditional Operator on the Left-Hand Side of an Assignment
Get Os Version/Friendly Name in C#
Why Are the Properties of Anonymous Types in C# Read-Only
Will the Dynamic Keyword in C#4 Support Extension Methods
Generics - Where T Is a Number
Finding Quoted Strings with Escaped Quotes in C# Using a Regular Expression
How to Find Default Web Browser Using C#
Get Property Name and Type Using Lambda Expression