MS Graph - LINQ 查询返回不正确的结果 [英] MS Graph - LINQ query returning incorrect results

查看:79
本文介绍了MS Graph - LINQ 查询返回不正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:以下返回一个不正确的值.我可能遗漏了什么以及如何纠正?这个问题似乎纯粹与 LINQ 的使用有关,而不是 MS Graph.

备注:虽然这是一个比较简单的案例,只有两种类型的值(Azure AD 和 MS 帐户),但在实际场景中会不止两种.因此,对于一个简单的 case,我们不能只使用一个简单的三元运算符(例如 condition ? consequent :alternative)——相反,它必须嵌入多个 case.如以下 LINQ 查询所示,我将有两个以上的案例.

LINQ:

dgrdUsers 是下面显示的 DataGrid 的名称.

Microsoft.Graph.IGraphServiceUsersCollectionPage users = await graphClient.Users.Request().Select("displayName, userPrincipalName, userType").GetAsync();列出<用户>lstUsers = (List)users.CurrentPage.ToList();dgrdUsers.ItemsSource = (来自 lstUsers 中的用户选择新的{DisplayName = User.DisplayName,UserPrincipalName = User.UserPrincipalName,用户类型 = 用户.用户类型,来源 =((User.UserType == "Member" && User.UserPrincipalName.Contains("#Ext#") == false) ?Azure Active Directory":(User.UserType == "Member" && User.UserPrincipalName.Contains("#Ext#")) ?微软账户":(User.UserType == Guest" && User.ExternalUserState == Accepted")?外部 Azure Active Directory":(User.UserType == Guest" && User.ExternalUserState == PendingAcceptance")?受邀用户":未知")}).ToList();

Ref:有类似的 LINQ 示例,例如

解决方案

如评论中所述:这是由字母大小写错误引起的,在代码中使用 #Ext# 但用户名"是 #EXT# 大写.

因此只需将代码中的 #Ext# 更改为 #EXT#.

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.

解决方案

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.

这篇关于MS Graph - LINQ 查询返回不正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆