API为json数据返回数组格式的数据循环

时间:2017-04-03 06:17:53

标签: json list api

我的编码如下,我已经给出了我现在得到的输出和我想要的预期输出,请通过它。

  public List<CompanyDto> GetCompanyData()
        {
            List<CompanyDto> resultCompanyDtoViews = new List<CompanyDto>();

            var viewCompanyDtoData = db.ds_CompanyDto.AsQueryable().AsNoTracking().ToList();

            var companyDtoData = viewCompanyDtoData .Select(a => new CompanyDto()
            {
                Id = a.Id,
                CompanyId=a.CompanyId,
                CompanyName=a.CompanyName,
            }).GroupBy(a => a.CompanyId).Select(a => a.First()).ToList();

            foreach (var companyData in companyDtoData)
            {
                CompanyDto companyDto = new CompanyDto();
                companyDto.Id = companyData.Id;
                companyDto.CompanyId=companyData.CompanyId;
                companyDto=companyData.CompanyName

                companyDto.countries =
                     viewCompanyDtoData.Where(a => a.CompanyId == companyData.CompanyId).Select(a => new CountryDto()
                     {
                         Country = a.Country
                     }).ToList();
                resultCompanyDtoViews.Add(companyDto);
            }


            return resultCompanyDtoViews;
        }

-----------------------------------Coding in DTO class
 public class CompanyDto
    {
        [Key]
        public Nullable<long> Id { get; set; }
        public int CompanyId { get; set; }        
        public string CompanyName { get; set; }
        public List<CountryDto> countries { get; set; }
    }
public class CountryDto
{
public string Country { get; set; }
}
----------------------Out put Now getting
{
  "value": [
    {
      "Id": 1,
      "CompanyId": 1,
      "CompanyName": "TCS",
      "countries": [
        {
          "Country": "3st Country"
        },
        {
          "Country": "4st Country"
        },
        {
          "Country": "6st Country"
        }
      ]
    }
  ]
}
--------------------------Expected Output 

{
  "value": [
    {
      "Id": 1,
      "CompanyId": 1,
      "CompanyName": "TCS",
      "countries": [
        "3st Country",
        "4st Country",
        "6st Country"
      ]
    }
  ]
}


Please help me out on this

我的预期json应该像最后一个预期的json一样,请帮我锄头来实现这个结果

0 个答案:

没有答案