How to return a general collection of lists in C #?

I have a linq to sql method, and when it executes the query, it returns some anonymous type.

I want to return this anonymous type back to my service level in order to do some kind of logic and stuff.

I don't know how to get it back though.

I thought I could do it

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 I'm not sure which build I am missing, or even if it is.

thanks

EDIT

Well, my first problem was resolved, but now I have a new problem that I'm not sure how to fix, since I know little about anonymous types.

I get this error

It is not possible to implicitly convert the type 'System.Collections.Generic.List' to 'System.Collections.Generic.List

Here is the request

   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;
   }

this element has this

Anonymous Types:

a is new{string Prefix}
b is new{ 'a prefix, decimal? marks}
+5
4

- . , . .

, .

. Rick Strahl MSDN docs , :

. , , , . . , struct class .

, - , . Microsoft MSDN , - . , .

UPDATE chobo2: , - , , - int, "" - , :

public class ReturnValue
{
    int prefix { get; set; }
    decimal Marks { get; set; } 
}  

, 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();
}

: .Select :

     .Select(group => new { prefix = group.Key, marks = .... }

:

     .Select(group => new ReturnValue { prefix = group.Key, marks = .... }

, ReturnValue - , , .

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

? , Reflection, . , . , .

It is better to use a class or structure and use the values ​​there.

+2
source

Anonymous types are very limited, you can use them only in the scope of the method, if they are declared.

You can pass them as simple objects, but then you lose the type information. I have seen some solutions passing an anonymous type; but they use reflection to obtain properties at a later point.

0
source

All Articles