开发者

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


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?


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜