Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq conversion

I am using the following code to return an IList:

public IList<string> FindCodesByCountry(string country)
        {
            var query = from q in session.Linq<Store>()
                        where q.Country == country
                        orderby q.Code
                        select new {q.Code};

            return (IList<string>) query.ToList();
        }

However I keep getting this error:

Unable to cast object of type 'System.Collections.Generic.List1[<>f__AnonymousType01[System.String]]' to type 'System.Collections.Generic.IList`1[System.String]'.

What I am supposed to return here?

like image 475
vikasde Avatar asked Jan 23 '23 17:01

vikasde


2 Answers

as long as q.code is a string this should work: note that it is not creating an anonymous object, just the string is being selected.

    public IList<string> FindCodesByCountry(string country)
    {
        var query = from q in session.Linq<Store>()
                    where q.Country == country
                    orderby q.Code
                    select q.Code;

        return query.ToList();
    }
like image 107
John Boker Avatar answered Feb 27 '23 16:02

John Boker


Is there a reason you were selecting an anonymous type? If not try this...

    var query = from q in session.Linq<Store>()
                where q.Country == country
                orderby q.Code
                select q.Code;
like image 42
Jace Rhea Avatar answered Feb 27 '23 18:02

Jace Rhea