MS Graph - LINQ query returning incorrect results

梦想的初衷 提交于 2020-07-22 05:51:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/62926660/ms-graph-linq-query-returning-incorrect-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!