Lambada Expression not working?

Jan 8, 2010 at 4:54 PM

I am trying to use a Lambada expression to dynamically build my query. When I run the application nothing is returned if I use the lambada expression. It looks to me as if it is not carrying the values through. Here is my code maybe someone can tell me what i am doing wrong or if this is a linq to ad issue?

 

private static IEnumerable<Demo.User> GetADUsers(string FName, string LName, string Logon)
        {
            
                                    
            IEnumerable<Demo.User> users;

            var usersDS = new DirectorySource<Demo.User>(ROOT, SearchScope.Subtree);


            users = from usr in usersDS
                    where usr.FirstName.StartsWith(FName)
                    select usr;
            
            users = users.Where(user => user.LastName.StartsWith(LName));
            users = users.OrderBy(user => user.MI).ToList(); // Sort them alphabetically by name.

            return users;
        }

Jan 12, 2010 at 2:31 PM

IEnumerable<Demo.User> users;

            var usersDS = new DirectorySource<Demo.User>(ROOT, SearchScope.Subtree);


            users = from usr in usersDS
                    where usr.FirstName.StartsWith(FName) && usr.LastName.StartsWith(LName)
                    select usr;

            return users;


figured it out.

Coordinator
Jul 14, 2010 at 5:23 AM

The upcoming refresh of LINQ to AD will support multiple where clauses. In the meantime, use && as you discovered.

Hope this helps,
-Bart

Jul 21, 2010 at 3:18 PM

Hi,

Is it possible to make some clauses conditional?
I try to use 1 query for querying against a few attribtues. Until now, I set a wildchar to fields which should not filter the result (when not provided by the user). But unfortunately some of the attributes I am querying do not always exist.
Is there a workaround?

Martin

 

 

 

 

 

Jul 22, 2010 at 1:26 PM

I solved the problem for me until there is a refresh with the new features.

Maybe it helps someone.

I modified the BuildPredicate method a little bit:

        private void BuildPredicate(LambdaExpression q)
        {
            StringBuilder sb = new StringBuilder();

            //
            // Recursive tree traversal to build the LDAP query (prefix notation).
            //
            ParsePredicate(q.Body, sb);

            //Original Code:
            //query = sb.ToString();

            //Modified Code: 
            if (query != null && query.Length != 0)
            {
                query = string.Format("(&{0}{1})", query, sb.ToString());
            }
            else
            {
                query = sb.ToString();
            }
        }
Now I can query like this:
...	
var person = new DirectorySource<PersonEntity>(_rootDirectoryEntry, System.DirectoryServices.SearchScope.Subtree);
IQueryable<EmployeeEntity> query = employees;

query = query.Where(x => x.Lastname == "Willis");
query = query.Where(x => x.Firstname == "Bruce");

query.Select....

Warning, the code ist not tested a lot!
Martin
Coordinator
Jul 23, 2010 at 8:21 PM

This is more or less how things will change - though an LDAP-specific tree model will be used to avoid magic format string throughout the code base (and such a tree model has lots of other advantages, too).