Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a list from a method in C#

Tags:

c#

list

linq

How can I return a list that contains the result of a LINQ to SQLquery? I'm trying this implementation, but I got this error.

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<object>

Any help would be appreciated.

public List<Object> getShoes() 
{
    var query = from b in db.BrandTbls.AsQueryable()
                join m in db.ShoeModelTbls on b.BrandID equals m.BrandID 
                join s in db.ShoeTbls on m.ModelID equals s.ModelID 
                join i in db.ShoeImageTbls on s.ShoeID equals i.ShoeID 
                select new { s.ShoeID, s.Size, s.PrimaryColor, s.SecondaryColor, s.Quantity, m.ModelName, m.Price, b.BrandName, i.ImagePath };

    return query.ToList();
}
like image 869
Tartar Avatar asked May 22 '14 20:05

Tartar


People also ask

Can you return a list in C?

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

Can I return 2 values from a function in C?

In C or C++, we cannot return multiple values from a function directly. In this section, we will see how to use some trick to return more than one value from a function. We can return more than one values from a function by using the method called “call by address”, or “call by reference”.

Can a function return an array?

Although functions cannot return arrays, arrays can be wrapped in structs and the function can return the struct thereby carrying the array with it.


1 Answers

Anonymous types are specifically designed to be used entirely within the scope in which they are defined. If you want to return the results of the query out from this method, you should create a new named type to represent the results of your query and select instances of that named type, not instances of an anonymous type.

like image 115
Servy Avatar answered Oct 15 '22 08:10

Servy