首页 > 解决方案 > 在 MS Graph API v1.0 中查找用户时的变量插值问题

问题描述

我正在使用以下代码来搜索用户详细信息:

 var users2 = await graphServiceClient.Users.
   .Request()
   .Filter("startswith(displayName,'Robert')")
   .Select(u => new {
       u.DisplayName,
       u.MobilePhone,
       u.UserPrincipalName
   }).GetAsync();

它可以工作,但是当我使用下面的代码并尝试将用户显示名称存储在字符串中然后运行查询时,它返回 null。

 string dispName = mem.DisplayName;

    var users2 = await graphServiceClient.Users
   .Request()
   .Filter("startswith(displayName,'{dispName}')")
   .Select(u => new {
       u.DisplayName,
       u.MobilePhone,
       u.UserPrincipalName
   })
   .GetAsync();

我也试过 $'{dispName}' ,它不起作用。请帮忙。

标签: c#azure-active-directorymicrosoft-graph-apiinterpolation

解决方案


根据$ - 字符串插值,它应该是这样的

    string dispName = mem.DisplayName;

    var users2 = await graphServiceClient.Users
   .Request()
   .Filter($"startswith(displayName,'{dispName}')")
   .Select(u => new {
       u.DisplayName,
       u.MobilePhone,
       u.UserPrincipalName
   })
   .GetAsync();

请参阅$filter以了解有关过滤器参数的更多信息。


推荐阅读