Below method selects admin rights and returns bool from a cached DataTable, would it perform better with Linq?
You might ask why don’t you test it. Well,due to lack of knowledge i can’t write it in Linq
DataRow[] result = PrivilegeMap.Select("privilegeActionId=" + (int)actionId);
bool moderatorHasIt = Convert.ToBoolean(result[0]["moderatorHasIt"]);
bool adminHasIt = Convert.ToBoolean(result[0]["adminHasIt"]);
if (myRole == User.Role.Admin)
{
return Convert.ToBoolean(adminHasIt);
}
if (myRole == User.Role.Moderator)
{
return Convert.ToBoolean(moderatorHasIt);
}
else
{
return false;
}
It probably would be faster using LINQ because the datatable wouldn’t have to parse your query string, but you’ll get the greatest advantage by changing the data representation to avoid querying against a datatable in the first place.
Create an
IDictionary<int, bool>that represents the privileges you’re interested in, keyed byactionId. Then when you need to do a lookup, you can just returndict[actionId.Regardless, I suspect this is probably a case of Premature Optimization: have you tested your program and found that this piece of code represents a significant portion of your processing time?