【问题标题】:How to get an IDS list of a Json type object with LINQ如何使用 LINQ 获取 Json 类型对象的 IDS 列表
【发布时间】:2018-09-27 18:36:49
【问题描述】:

如果您希望获得带有 Acl 类的 IDS 的列表,问题是这些都包含在以下公司列表中...

公司:CS

public class Company
{
    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }      

    [JsonProperty(PropertyName = "acl")]
    public List<Acl> Acl { get; set; }
}

ACL.CS:

public class Acl
{
    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }      

    [JsonProperty(PropertyName = "actions")]
    public List<Action> Actions { get; set; }
}

这些对象是 JSON 类型,具有以下结构

图像显示了您想要获取的示例,包含 COMPANY 的 ACL 对象的 IDS 列表

但是我怎样才能得到这个结果呢?我目前有以下查询

var servicios = mainViewModel.LoginResponse.Companies
                    .Where(c => c.Principal == true)
                    .Select(c => c.Acl)
                    .ToList();

如何隔离我的对象并仅获取所需 IDS 的列表?我必须使用什么 LINQ 命令?对我有什么帮助吗?

【问题讨论】:

    标签: c# list performance linq linq-to-sql


    【解决方案1】:

    试试这个:

    var servicios = mainViewModel.LoginResponse.Companies
                    .Where(c => c.Principal == true)
                    .SelectMany(c => c.Acl.Select(z => z.Id))
                    .ToList();
    

    【讨论】:

      猜你喜欢
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      相关资源
      最近更新 更多