Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

c# - MS Graph - LINQ query string NOT NULL or Empty issue

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:

enter image description here

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

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...