Reputation: 7766
I am trying to convert the result of a linq statement into string[] array like below
string[] g = from p in db.np_user_security where
p.user_id == user_id select p.group_id.Distinct().ToArray();
but giving me an error as below
Cannot implicitly convert type 'System.Linq.IQueryable' to 'string[]'
The query return a list of ids and I need to save it in array of string or long. What Am I missing?
Upvotes: 0
Views: 1322
Reputation: 89325
By doing p.group_id.Distinct().ToArray()
you split each group_id
into array of distinct characters. So in fact, the entire query returns IQueryable
of array of characters, not ids.
I believe you want to wrap the query with parens first before calling Distinct().ToArray()
:
string[] g = (from p in db.np_user_security
where p.user_id == user_id
select p.group_id
).Distinct().ToArray();
Upvotes: 2