How to return an anonymous type in linq-to-sql

I have the following dummy code, but the idea is to return a generic class

public static var getSeaOf(string user) { var periodsSettings = from p in sea where p.add_by == new Guid(user) select new { p.id, p.name }; return var; } 

I read here - How to return an anonymous type from a C # method that uses LINQ to SQL , the best solution for this case is to create a class to return the type.

But my question is: if I have hundreds of such functions, does this mean that I need to have hundreds of classes?

I hope there is a more general solution, thanks for your help !!


Release

I look

Silverlight - LinqToEntities - How to return anonymous types

But I can not specify the class name in select new, how does the article do this?

 public static IEnumerable<retSea> getBskSeasonsOf(string user) { var periodsSettings = from p in sea where p.add_by == new Guid(user) select new retSea { p.id, p.name }; return periodsSettings; } 
+6
c # linq linq-to-sql
source share
5 answers

Here's a blog post about anonymous type names in .NET using the VS 2010 Generate From Usage feature.

http://diditwith.net/2009/10/24/NamingAnonymousTypesWithGenerateFromUsage.aspx

+2
source share

If I remember correctly, the specification says that the anonymous type generated for this object cannot escape the method in which it is defined. Therefore, the only method that can ever have variables of this type is the method in which the object is instantiated. a bit sketchy when you think that a LINQ query can be compiled into a bunch of methods, but that's magic.

The object itself, however, can avoid this method. To make this work, you need ... return object . You will have to access it using reflection (or dynamic ), so that you lose type safety. Perhaps you should consider whether it is worth it or not. Most likely no. And, most likely, you also do not have hundreds of different types of results. I am sure many of your queries return the same data type. Reuse these classes.

+3
source share

If you really have hundreds of classes, just make a class. Or use something already built-in for a pair of key values, such as KeyValuePair.

+3
source share

If each of these methods returns an anonymous type that has the same fields as the others, then you only need to create one class and reuse it in all methods.

If each of them returns an anonymous type with different fields, then yes, you will need to create a class for each of these methods.

If you are using C # 4.0, you can try using a dynamic type and see what problems you can get yourself there.

+2
source share

Well, you probably want to return periodsSettings .

But after that, consider enumerating the collection using ToDictionary() inside this method, where the key is p.id and the value is p.name .

 public static IDictionary<int, string> getSeaOf(string user) { return (from p in sea where p.add_by == new Guid(user)) .ToDictionary(p => p.id, p => p.name); } 

This effectively gives you data without having to create a new class. He lists it here, of course, but that may not matter in your case.

0
source share

All Articles