简体   繁体   中英

How can you use LINQ to find Azure AD users with specific licenses using the Azure AD Graph API Client Library 2.0

I started with the sample .NET Graph API Console app which I got working. I want to be able to query Azure AD for all users with a specific license. I think I have to do some sort of nested LINQ query.

This was my first attempt

IUserCollection userCollection = activeDirectoryClient.Users;

            searchResults = userCollection.Where(user =>
               user.AssignedPlans.Where(plans => plans.Service.Contains("exchange")) &&
               user.DisplayName.StartsWith(searchString)).ExecuteAsync().Result;

            usersList = searchResults.CurrentPage.ToList();

I got an error that says "Error 3 Operator '&&' cannot be applied to operands of type 'System.Collections.Generic.IEnumerable' and 'bool"

I tried changing the .Where to .Any and got it compile.

searchResults = userCollection.Where(user =>
                   user.AssignedPlans.Any(plans => plans.Service.Contains("exchange")) &&
                   user.DisplayName.StartsWith(searchString)).ExecuteAsync().Result;

but then i get this error in the console when I run the code.

Error getting User Expression of type 'System.Collections.Generic.IList`1[Microsoft.Azure.ActiveDirectory.GraphClient.Internal.AssignedPlan]' cannot be used for
 parameter of type 'System.Collections.Generic.IEnumerable`1[Microsoft.Azure.ActiveDirectory.GraphClient.AssignedPlan]' of method 'Boolean Any[AssignedPlan](Sys
tem.Collections.Generic.IEnumerable`1[Microsoft.Azure.ActiveDirectory.GraphClient.AssignedPlan], System.Func`2[Microsoft.Azure.ActiveDirectory.GraphClient.Assig
nedPlan,System.Boolean])'

Users can have many assigned plans. I need to return all the users that have at least one assigned plan with a service that is equal to "exchange"

Couple of things going on here - and I'm afraid I don't have any decent answers for you. Firstly our client library doesn't support the kind of construct you are attempting, but I believe we're fixing that pretty soon. A bigger issue is that our service side doesn't support querying of a multi-valued complex type (AssignedPlan in this case). I will file a feature request for this item.

The only workaround that we have available at this time is that you attempt to filter down the result set using whatever we support today (see http://blogs.msdn.com/b/aadgraphteam/archive/2014/02/11/support-for-disjunctive-or-filter-clauses-in-the-graph-directory-service.aspx or the equivalent through the client library), and then do a client side filter. Not ideal...

One other thing that we think might help is if we offered an Any(*) filter, that allows you to filter on a property that is set. That way you could filter down to those users that have assignedPlans, and go from there.

Let me know what you think

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM