Lasse Edsvik
Lasse Edsvik

Reputation: 9298

IQueryable to string[] array?

I have this piece of code:

    public string[] GetUsergroupRoles(string username)
    {
        var qry = from ug in _entities.Usergroups
               from r in _entities.Roles
               from u in _entities.Users
               where u.Username == username
               group r by
               new
               {
                   r.RoleID
               };

        return qry.ToArray();
    }

I get error "...Data.Models.Role>[]' to 'string[]'. An explicit conversion exists (are you missing a cast?)"

whats needed to make it return an array of strings?

/M

Upvotes: 1

Views: 5260

Answers (3)

Doug
Doug

Reputation: 6442

Try iterating through qry and accumulate the result of ToString method of each item into a string[] variable, then returning it. Something like this:

// your code here

string[] ret = new string[qry.Length];
int i = 0;
foreach (var item in qry) {
  ret[i] = qry.ToString()
  i++;
}
return ret;

Upvotes: 1

Gishu
Gishu

Reputation: 136613

You are trying to cast a SomeType[] to a string[] and the compiler is complaining that it doesn't know how since you haven't defined a cast.

Try looping over the array and collection the specific string attribute of SomeType (or ToString() equivalent) into an array.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500225

Well, you're creating a sequence of groups (and I'm not sure why you're using an anonymous type to do so - you should be able to just use group r by r.RoleID).

What string would you expect from each group?

Upvotes: 3

Related Questions