Query JSON using LINQ

前端 未结 2 511
自闭症患者
自闭症患者 2021-02-15 12:03

I have a Json response that I receive from an API call. It has several nested levels as show below (this is a snippet):

\"Items\": [
  {
    \"Result\": {
               


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-15 12:34

    To start off, I'm not familiar with writing LINQ/LAMBDA using this format ["some"]["thing"]. My first step would be to create some classes/objects to house the data in to ease creating any code afterwards.

    e.g.

    public class Result
    {
        public Guid Id { get; set; } 
        public string Name { get; set; },
        public DateTime StartDate  { get; set; }
        //you get the idea
    }
    

    But possibly try the following?

        var result = from p in data["Data"]["Items"].Children()
                     where (bool)p["Result"]["ServiceAgreement"]["AdditionalItems"]["ScheduleBased"] == true
                        && (p["Result"]["ServiceAgreement"]["AdditionalItems"]["PayCycle"]).Where(o => o.["ScheduleBased"] == true)
                     select new
                     {
                         Name = (string)p["Result"]["Client"]["Name"],
                         Id = (string)p["Result"]["Client"]["Id"]
                     };
    

提交回复
热议问题