MS Graph-LINQ查询字符串NOT NULL或为空 [英] MS Graph - LINQ query string NOT NULL or Empty issue
问题描述
如下所示,对于从<迁移的用户,我的 Azure Portal
正确地将 Source
列值显示为 Windows Server AD
.code> Windows Active Directory 到 Azure Active Directory
.
Azure门户中显示的用户:
现在在我的 WPF
应用程序中,我正在使用
问题:为什么上述 LINQ
查询以 Azure Active Directory
的形式返回 Source
列值 Windows Server AD
.除非我在这里没有东西,否则这似乎是与LINQ相关的问题.在此处和解决方案
进行事务比较的顺序.它将在第一个匹配的对象上停止,而不是最后一个匹配的对象.因此,如果您希望用户键入没有 OnPremisesUserPrincipalName
的Member,则选择"Windows Server AD";不管什么 UserPrincipalName
是什么,您都需要将该检查移至第一个.另外,由于您已经检查了#EXT#",在 UserPrincipalName
中,您不必在下一行再次检查它.
源=((User.UserType =="Member"&&!string.IsNullOrEmpty(User.OnPremisesUserPrincipalName))?"Windows Server AD":User.UserType =="Member"&&!User.UserPrincipalName.Contains(#EXT#"")?"Azure Active Directory":User.UserType =="Member"?"Microsoft帐户":User.UserType ==来宾"&&User.ExternalUserState ==已接受";?外部Azure Active Directory":User.UserType ==来宾"&&User.ExternalUserState =="PendingAcceptance";?受邀用户":未知";)
As shown below, my Azure Portal
is correctly displaying the Source
column value as Windows Server AD
for the users that were migrated from Windows Active Directory
to Azure Active Directory
.
Users shown in Azure Portal:
Now in my WPF
app, I am using Microsoft Graph to display the same list in a DataGrid
. But the following LINQ
query is still displaying the Source
column value of the migrated users as Azure Active Directory
instead of Windows Server AD
:
var users = await graphClient.Users
.Request()
.Select("displayName, userPrincipalName, onPremisesUserPrincipalName, userType")
.GetAsync();
List<User> lstUsers = (List<User>)users.CurrentPage.ToList();
//I noticed the following Linq without lambda works better if select case staement have multiple conditions: https://stackoverflow.com/a/936136/1232087
var userslist =
(
from User in lstUsers
select new
{
DisplayName = User.DisplayName,
UserPrincipalName = User.UserPrincipalName,
OnPremisesUserPrincipalName = User.OnPremisesUserPrincipalName,
UserType = User.UserType,
Source =
(
User.UserType == "Member" && !User.UserPrincipalName.Contains("#EXT#") ? "Azure Active Directory" :
User.UserType == "Member" && User.UserPrincipalName.Contains("#EXT#") ? "Microsoft Account" :
User.UserType == "Guest" && User.ExternalUserState == "Accepted" ? "External Azure Active Directory" :
(User.UserType == "Member" && string.IsNullOrEmpty(User.OnPremisesUserPrincipalName) == false) ? "Windows Server AD" :
User.UserType == "Guest" && User.ExternalUserState == "PendingAcceptance" ? "Invited user" : "Unknown"
)
}
);
DataGrid display of the above query result in my WPF app:
According to above LINQ query, values inside red should have displayed as Windows Server AD
because OnPremisesUserPrincipalName
value of the query (shown in On Prem Name
column below) are not null
Question: Why the above LINQ
query is returning the Source
column value as Azure Active Directory
instead of Windows Server AD
. This seems to be a LINQ related issue unless I'm missing something here. There are similar LINQ examples here and here.
The order that you do your comparisons in matters. It will stop on the first one that matches, not the last one. So if you want user type Member with no OnPremisesUserPrincipalName
to select "Windows Server AD" reguardless of what UserPrincipalName
is then you need to move that check up to the first one. Additionally since you already check for "#EXT#" in UserPrincipalName
you don't have to check it again on the next line.
Source =
(
(User.UserType == "Member" && !string.IsNullOrEmpty(User.OnPremisesUserPrincipalName))
? "Windows Server AD" :
User.UserType == "Member" && !User.UserPrincipalName.Contains("#EXT#")
? "Azure Active Directory" :
User.UserType == "Member"
? "Microsoft Account" :
User.UserType == "Guest" && User.ExternalUserState == "Accepted"
? "External Azure Active Directory" :
User.UserType == "Guest" && User.ExternalUserState == "PendingAcceptance"
? "Invited user" : "Unknown"
)
这篇关于MS Graph-LINQ查询字符串NOT NULL或为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!