[ACCEPTED]-How to return a list from a method in C#-list

Accepted answer
Score: 13

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

Score: 3

You can't return a List where T is an anonymous 4 type; you need to return a known type. You 3 can create a class

public class MyResult
{
    public string ShoeID {get; set;}
    public string Size {get; set;}
    public string PrimaryColor {get; set;}
    public string SecondaryColor {get; set;}
    public string Quantity {get; set;}
    public string ModelName {get; set;}
    public string Price {get; set;}
    public string BrandName {get; set;}
    public string ImagePath {get; set;}
}

then change your select 2 like so

select new MyResult { ShoeID = s.ShoeID, Size = s.Size, PrimaryColor = s.PrimaryColor ........ };

and your method signature would change 1 to

public List<MyResult> getShoes() 

More Related questions