【问题标题】:how to get the value using linq query C#如何使用 linq 查询 C# 获取值
【发布时间】:2020-12-31 21:03:49
【问题描述】:

如何使用 linq 查询或 lambda 表达式从 json 下面获取 authRoleCode= AHGENERAL 的值。在代码下方,我在此 json 下方提供了类结构。我试过这样。

var str = request.ApplicationsAccess.CategoryAccessType.Where(x=>x.), 

如何获取它的数据数据。

    "applicationsAccess": {
                            "categoryAccessType": [{
                                            "categoryCode": "Accident and Health",
                                            "basicApplications": {
                                                            "applictaionType": "basicApplications",
                                                            "roleAmpLabelAdded": [{
                                                                            "roleAMPLabel": "General access to marketing & resources",
                                                                            "authRoleCode": "AHGENERAL",
                                                                            "authRoleName": "A&H - General access to marketing & resources".

这是类结构,我们用于上面的json。

public partial class ApplicationsAccess
    {
        [JsonProperty("categoryAccessType")]
        public List<CategoryAccessTyp> CategoryAccessType { get; set; }
    }

    public partial class CategoryAccessTyp
    {
        [JsonProperty("categoryCode")]
        public string CategoryCode { get; set; }

        [JsonProperty("applicationTypes")]
        public List<ApplicationType> ApplicationTypes { get; set; }
    }

    public partial class ApplicationType
    {
        [JsonProperty("applictaionType")]
        public string ApplictaionType { get; set; }
       
        public List<RoleAmpLabelAdded> RoleAmpLabelAdded { get; set; }
        
        public ApplicationsAdded ApplicationsAdded { get; set; }
    }

    public partial class ApplicationsAdded
    {
        [JsonProperty("applications")]
        public List<Applications> Applications { get; set; }
    }

    

    public partial class RoleAmpLabelAdded
    {
        [JsonProperty("roleAMPLabel")]
        public string RoleAmpLabel { get; set; }

        [JsonProperty("authRoleCode")]
        public string AuthRoleCode { get; set; }

        [JsonProperty("authRoleName")]
        public string AuthRoleName { get; set; }

        [JsonProperty("applications")]
        public List<Applications> Applications { get; set; }
    }
    public partial class Applications
    {
        [JsonProperty("appId")]

        public long AppId { get; set; }

        [JsonProperty("appCode")]
        public long AppCode { get; set; }

        [JsonProperty("appName")]
        public string AppName { get; set; }
    }

如果您需要更多信息,请告诉我们 TIA。

【问题讨论】:

  • request 对象是什么?是否将 JSON 解析为对象模型?而且我不太明白什么是行不通的。您能否提供您正在做什么的代码,request 对象是如何初始化的。此外,如果您也可以提供错误消息,那就太好了。
  • @E.Shcherbo 我添加了类结构。
  • 谢谢。你究竟需要得到什么? RoleAmpLabelAdded 类的对象 AuthRoleCode 等于 "AHGENERAL"?
  • @E.Shcherbo 是的

标签: linq c#-4.0


【解决方案1】:

如果您需要AuthRoleCode 属性等于"AHGENERAL"RoleAmpLabelAdded 类的对象,那么您可以尝试以下代码:

 IEnumerable<RoleAmpLabelAdded> result = request
            .ApplicationAccess
            .CategoryAccessType
            .SelectMany(c => c.ApplicationTypes.SelectMany(t => t.RoleAmpLabelAdded))
            .Where(r => r.AuthRoleCode == "AHGENERAL");

获取所有AuthRoleCode-s:

       IEnumerable<string> result = request
            .ApplicationAccess
            .CategoryAccessType
            .SelectMany(c => c.ApplicationTypes.SelectMany(t => t.RoleAmpLabelAdded))
            .Select(r => r.AuthRoleCode);

【讨论】:

  • 感谢您的回答。我们可以得到所有的 AuthRoleCode 吗?
  • 当然,您只需再拨打Select 一次即可。我会更新我的答案。
  • 我想要所有的 authrole 代码,而不是 AuthRoleCode == "AHGENERAL"。
  • @Sam 这有帮助吗?
猜你喜欢
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 2010-09-19
  • 1970-01-01
相关资源
最近更新 更多