问题
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