I am getting the following error on a LINQ query:
LINQ to Entities does not recognize the method 'System.String getGroupOwner(System.String)' method, and this method cannot be translated into a store expression.
I found this issue which seems to be very similar: LINQ to Entities does not recognize the method
I know the problem is that LINQ needs to convert the statment into SQL and the function can not be included but I am struggling figuring out how to rewrite this.
Here are my 2 functions:
public IQueryable<Group> GetGroups()
{
string userEID = HttpContext.Current.Request.LogonUserIdentity.Name.ToUpper();
return db.Groups.Where(sg => sg.GroupActive == true && userEID == getGroupOwner(sg.GroupSecurity)).OrderBy(g => g.GroupName);
}
private string getGroupOwner(string GName)
{
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, GName);
System.DirectoryServices.DirectoryEntry de = (System.DirectoryServices.DirectoryEntry)group.GetUnderlyingObject();
var owner = de.Properties["managedBy"];
return owner.Value.ToString().ToUpper();
}
Any help would be greately appreciated! Thanks in advance.
EDIT #1:
Thanks for the comment @DevilSuichiro. Here is my first revision which I think is a little closer. Is this on the right track? What is the syntax for removing the item.
public IQueryable<Group> GetGroups()
{
string userEID = HttpContext.Current.Request.LogonUserIdentity.Name.ToUpper();
var rs = db.Groups.Where(sg => sg.GroupActive == true).OrderBy(g => g.GroupName);
foreach (var gname in rs)
{
var groupOwner = getGroupOwner(gname.GroupSecurity);
if (groupOwner != userEID)
{
//I think I need to remove gname from rs here - What is the proper syntax?
}
}
return rs;
}