nam
nam

Reputation: 23868

MS Graph - LINQ query returning incorrect results

Question: Following is returning one incorrect value. What I may be missing and how can it be corrected? The issue seems to be purely related to use of LINQ and not MS Graph.

Remark: Although this is a simpler case with only two types of values (Azure AD and MS Account), in real scenarios there will be more than just two cases. Hence, we can't just use a simple ternary operator (e.g. condition ? consequent : alternative) for a simple case - instead, it has to be embedded with multiple cases. I'll have more than two cases as the following LINQ query shows.

LINQ:

dgrdUsers is the name of the DataGrid shown below.

Microsoft.Graph.IGraphServiceUsersCollectionPage users = await graphClient.Users.Request()
    .Select("displayName, userPrincipalName, userType")
    .GetAsync();

List<User> lstUsers = (List<User>)users.CurrentPage.ToList();

dgrdUsers.ItemsSource = (
            from User in lstUsers
            select new
            {
                DisplayName = User.DisplayName,
                UserPrincipalName = User.UserPrincipalName,
                UserType = User.UserType,
                Source =
                (
                    (User.UserType == "Member" && User.UserPrincipalName.Contains("#Ext#") == false) ? "Azure Active Directory" :
                    (User.UserType == "Member" && User.UserPrincipalName.Contains("#Ext#")) ? "Microsoft Account" :
                    (User.UserType == "Guest" && User.ExternalUserState == "Accepted") ? "External Azure Active Directory" :
                    (User.UserType == "Guest" && User.ExternalUserState == "PendingAcceptance") ? "Invited user" : "Unknown"
                )
            }
        ).ToList();

Ref: There are similar LINQ examples such as this and this

Resulted DataGrid:

The Source column value in second row should be Microsoft Account.

enter image description here

Upvotes: 2

Views: 449

Answers (1)

Hury Shen
Hury Shen

Reputation: 15754

As mentioned in comments: It was caused by incorrect letter case, use #Ext# in code but the "User Name" is #EXT# in upper case.

So just change the #Ext# to #EXT# in code.

Upvotes: 3

Related Questions