how to return an anonymous type in linq-to-sql
I have the following bogus 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 have read here - How to return anonymous type from c# method that uses LINQ to SQL that the best solution for this case is 开发者_如何学运维to create a class for the return type.
But my question is if I have hundreds of functions like this does it mean I need to have hundreds of classes?
I hope there is a more generic solution, thanks for your help!!
Edition
I take a look at
Silverlight - LinqToEntities - How Do I Return Anonymous Types
But I cannot specified the class name in the select new, like the article does?
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;
}
If I remember correctly, the spec says that the anonymous type generated for that object cannot escape the method it's defined in. Therefore the only method that could ever have variables of that type is the method the object is instantiated in. This gets a bit sketchy when you consider the fact that the LINQ query could get compiled into a bunch of methods, but that's magic.
The object itself, however, can escape the method. The way to make this work is to... return object
. You'll have to access it using reflection (or dynamic
) though, so you'll lose type safety. You might want to consider whether this is worth it or not. Most likely it's not. And most likely you don't have hundreds of different types of results either - I bet many of your queries return the same type of data. Re-use those classes.
If you really have hundreds of classes just like that, just make a class. Or use something already build in for a key value pair, like KeyValuePair.
Here's a blog post on naming anonymous types in .NET using VS 2010's Generate From Usage functionality.
http://diditwith.net/2009/10/24/NamingAnonymousTypesWithGenerateFromUsage.aspx
If each one of those methods returns an anonymous type that has the same fields as the others, then you only have to create one class and re-use it throughout the methods.
If they each return an anonymous type that has different fields, then yes, you'll have to create a class for each of those methods.
If you're using C# 4.0, you could attempt to take advantage of the dynamic type and see what kind of trouble you could get yourself into there.
Well, you probably want to return periodsSettings
.
But, after that, consider enumerating the collection with ToDictionary()
within 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 gets you your data without having to make a new class. It enumerates it here, of course, but that might not matter in your case.
精彩评论