0

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;
}
Community
  • 1
  • 1
Nate23VT
  • 423
  • 8
  • 27
  • db.Groups.Where signals EF to create an SQL query from the following. However, EF can't map your custom function to SQL. execute the query, then map those with your function – DevilSuichiro Oct 30 '15 at 17:53
  • Instead of removing items from the rs collection, why not add valid items to another collection which can be returned? – Kevin Oct 30 '15 at 19:05

1 Answers1

0

You can't remove items from an IEnumerable while you are enumerating them that will produce an error. What you can do however is select all the elements which you want and return those. Something like this.

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);
    var toReturn = new List<Group>();
    foreach (var gname in rs)
    {
            var groupOwner = getGroupOwner(gname.GroupSecurity);
            if (groupOwner == userEID)
            {
                toReturn.Add(gname);
            }
    }
    return toReturn.AsQueryable<Group>();
}
Callback Kid
  • 708
  • 5
  • 22
  • I was very close to this solution before I saw this response. This worked great - thanks! – Nate23VT Oct 30 '15 at 20:34
  • You could also use LINQ to include only the items you want returned: `var toReturn = rs.ToList(); return toReturn.Where( g => getGroupOwner(g.GroupSecurity) == userEID).AsQueryable();` I haven't tested this, though – Tony Vitabile Oct 30 '15 at 20:51
  • You are right Tony, he doesn't even need the `toReturn` he could simply do `return rs.ToList().Where(g => getGroupOwner(g.GroupSecurity) == userEID).AsQueryable();` – Callback Kid Oct 30 '15 at 21:02