How to return a list from a method in C#

c#, linq, list

Solution

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.

Problem

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

Original source

Related problems