I have some linq to sql method and when it does the query it returns some anonymous type.
I want to return that anonymous type back to my service layer to do some lo
You want to return an anonymous type from a regular method? I'm quite sure you can with Reflection, but there would be no type safety and a whole host of other problems. Not to mention it looks weird from the calling codes perspective. You would basically have to return object I think.
You would be better use a class or struct and stuff the values in there.
public List<T> thisIsAtest<T>()
{
return query;
}
Anonymous types are very limited, you can only use them in the scope of the method were they are declared.
You can pass them back as plain objects, but then you lose the type information. I have seen some solutions passing a anonymous type around; but they use reflection to retrieve the properties at a later point.
If you declare the generic list as a global variable, you don't need to use return. I used the return function in my code anyway simply because marc_s said i couldn't. lol
public static class Globals
{
public static List<string> CurPart = new List<string>();
}
public List<string> MSQueryRead1DArray(string sql, string db)
{
return Globals.CurPart;
}
You can't - period. You cannot use anonymous types outside their own scope, e.g. you cannot return them as return values from a method.
If you need to return them, you need to define a new concrete class instead of the anonymous type, and use that in the place of the anonymous type.
See Rick Strahl's blog post on the scoping of anonymous types, and see the MSDN docs here which clearly state:
An anonymous type has method scope. To pass an anonymous type, or a collection that contains anonymous types, outside a method boundary, you must first cast the type to object. However, this defeats the strong typing of the anonymous type. If you must store your query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.
OK, sure - there are dirty awful hacks to indeed return anonymous types. But if Microsoft's MSDN AND Jon Skeet discourage that practice, then - just don't do it. By definition and intention, anonymous types are bound to the method they're defined in.
UPDATE for chobo2: I don't know what your datatypes are - just guessing - but assuming "prefix" is an int and "marks" is a decimal, you could define a new class:
public class ReturnValue
{
int prefix { get; set; }
decimal Marks { get; set; }
}
and then your code would be a method that returns a List<ReturnValue>
:
public List<ReturnValue> thisIsAtest()
{
DbContext.Table.Where(u => u.Table.UserId == userId && u.OutOFF != 0)
.GroupBy(u => new { u.Table.Prefix })
.Select(group => new ReturnValue
{ prefix = group.Key,
Marks = group
.Sum(item => (item.Mark * item.Weight) / item.OutOFF) })
.ToList();
}
The key here is: in your .Select
method, instead of creating a new instance of an anonymous type:
.Select(group => new { prefix = group.Key, marks = .... }
you create an instance of a concrete type:
.Select(group => new ReturnValue { prefix = group.Key, marks = .... }
This way, you'll have a concrete class ReturnValue
- name that anything you like - and then you can easily return a list of that type, and use that type elsewhere, too.