views:

529

answers:

4

Hi

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 logic and stuff on it.

I don't know how to return it though.

I thought I could do this

public List<T> thisIsAtest()
{
     return query;
}

but I get this error

Error   1 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

So not sure what assembly I am missing or if that is even the case.

Thanks

EDIT

Ok my first problem was solved but now I have a new problem that I am not sure how to fix since I don't know much about anonymous types.

I get this error

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List

Here is the query

   DbContext.Table.Where(u => u.Table.UserId == userId && u.OutOFF != 0)
       .GroupBy(u => new { u.Table.Prefix })
       .Select(group => new { prefix = group.Key, 
                              Marks = group.Sum(item => (item.Mark * item.Weight) / item.OutOFF) })
       .ToList();

Edit 2

public class ReturnValue
{
   string prefix { get; set; }
   decimal? Marks { get; set; } 
}

public List<ReturnValue> MyTest(Guid userId)
{
   try
   {
       var result = dbContext.Table.Where(u => u.Table.UserId == userId && u.OutOFF != 0).GroupBy(u => new { u.Table.Prefix })
       .Select(group => new { prefix = group.Key, Marks = group.Sum(item => (item.Mark * item.Weight) / item.OutOFF) }).ToList();
       return result;
   }
   catch (SqlException)
   {
       throw;
   }

the select has this in it

Anonymous Types:

a is new{string Prefix}
b is new{ 'a prefix, decimal? marks}
+1  A: 
public List<T> thisIsAtest<T>()
{
     return query;
}
Yuriy Faktorovich
That solved that problem but now I get thisCannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<T>
chobo2
The query you're returning is a list of anonymous types, you can only return a list of <T>. You can use objects and casting hack by Jon Skeet, but it is strongly not recommended.
Yuriy Faktorovich
+5  A: 

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.

marc_s
Actually, you can: http://msmvps.com/blogs/jon_skeet/archive/2009/01/09/horrible-grotty-hack-returning-an-anonymous-type-instance.aspx Not that I'd recommend it though.
Mark Byers
Hmm can I see an example. I get this now. I don't want to do logic in my repository layer other wise I have to make 2 of the same methods because I use it in to place but need to use the data in 2 different ways.Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<T>
chobo2
@MarkByers: but Jon himself says: "don't use this code"...... so really, for all intents and purposes: **DON'T** - don't even try.
marc_s
This is not true. You can return instances of an anonymous type as `object`s. I'm not saying that's good practice, but it is possible. That is, `public object DoNotDoThis() { return new { FirstName = "Donald", LastName = "Knuth" }; }` is legal.
Jason
@Jason: yes, theoretically - but that defeats all type safety and everything. Don't do it - just create a real concrete class if you need to return it - much easier and cleaner.
marc_s
@marc_s: I completely agree with you. Hence the name of the sample method being `DoNotDoThis`. And as for saying "anonymous types are bound to the method they're defined in," that is not entirely accurate. Remember, the compiler emits a class definition for you and it's in the defining assembly. With some witchcraft (i.e., mucking around with IL or reflection) you can access and create instances of the type. This is also very, very bad practice and should never ever be done, but it's possible.
Jason
Well, I guess the official MSDN docs are pretty clear on what the intent of the language designers was - anonymous types were never intended or designed to be used outside the scope they were created in. That's what I'm trying to get across....
marc_s
Hey I tried what you have but it still does not work. I will post what I have.
chobo2
Ok problems seem to be solved. Thanks. Its good to know that it is not hard to get convert an anonymous type into something concrete.
chobo2
+1  A: 

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.

Skurmedel
A: 

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.

driis