Reputation: 21
I want to select multiple column in DB and store in array but keep getting this error
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
it should return me something like "FunctionCode-ActivityCode"
public override string[] GetRolesForUser(string username)
{
DEV_Context OE = new DEV_Context();
string role = OE.UserRefs.Where(x => x.UserName == username).FirstOrDefault().RoleName;
string[] result = OE.RolePermissionRefs
.Where(x => x.RoleName == role && x.StatusCode == "A")
.Select(x => new { FunctionCode = x.FunctionCode, ActivityCode = x.ActivityCode }.ToString())
.ToArray();
return result;
}
Upvotes: 0
Views: 3408
Reputation: 975
ToString() is available only in EF 6.0 and higher. In your case you could just call ToArray() before Select and do something like Selman suggested.
Upvotes: 0
Reputation: 101680
Even if it would work, it wouldn't give you the expected result.You are calling ToString
on an anonymous object. If you just want to concatenate values you can try:
string[] result = OE.RolePermissionRefs
.Where(x => x.RoleName == role && x.StatusCode == "A")
.Select(x => new { x.FunctionCode, x.ActivityCode })
.AsEnumerable()
.Select(x => string.Join("-", x.FunctionCode, x.ActivityCode))
.ToArray();
The reason why ToString
doesn't work is already explained in the error message.It can't be translated to SQL.So you need to load result into memory (e.g using AsEnumerable
in the above code) and then project them.
Upvotes: 1