创建一个lambda表达式来查询数据

时间:2019-06-18 11:23:17

标签: c# json lambda

我基于json数据创建了一些类,以便反序列化json数据。 我想使用lambda表达式访问sku和skuName,但我无法这样做。

因此设法获得了skuName,但是我如何获得sku属性?我想获取skuName属性以及关联的sku属性。 skuName和sku属于同一类-SKU类。谢谢您的时间

    var productSku = skuIdName.BodyText.products.vendors
         .SelectMany(x => x.listings
        .SelectMany(l => l.skus
        .SelectMany(f => f.skuName

         ))).ToArray();

我无法获得所有的skus和sdkuName(使用lambda),并使用sku值作为键并将skuName作为值将其放在列表或字典中,因为我以后想将这些值存储在数据库。 例如 “ sku”:“ SK8772”, “ skuName”:“国内和国际电话计划”, “ sku”:“ SK8265”, “ skuName”:“音频会议”,

     JSON DATA:

 {
   "Result": "Success",
   "BodyText": 
  {
  "products": 
{
  "totalProducts": 510,
  "recordsPerPage": 10,
  "page": 1,
  "totalPages": 51,
  "vendors": [
    {
      "vendorId": "397",
      "vendorName": "Microsoft",
      "listings": [
        {
          "listingName": "Office 365 Enterprise",
          "skus": [
            {
              "sku": "SK10228",
              "skuName": "Microsoft 365 E5 without Audio Conferencing",
              "description": "Pending",
              "zeroValueSku": "t",
              "manufacturerPartNumber": "db5e0b1c9cc3459c9d08c61993959fd3",
              "article": "4090153",
              "vendorMapId": "db5e0b1c-9cc3-459c-9d08-c61993959fd3",
              "billingType": "Monthly",
              "productType": "SaaS",
              "qtyMin": "1",
              "qtyMax": "",
              "addOns": [
                {
                  "sku": "SK8265",
                  "skuName": "Audio Conferencing",
                  "description": "For businesses that need to enable users to dial-in a number to join Skype meetings, or dial-out to bring participants into the meeting. There are base pre-requisites required to purchase this offering.",
                  "zeroValueSku": "t",
                  "qtyMin": "1",
                  "qtyMax": "",
                  "vendorMapId": "c94271d8-b431-4a25-a3c5-a57737a1c909",
                  "manufacturerPartNumber": "c94271d8b4314a25a3c5a57737a1c909",
                  "article": "3873033"
                },
                {
                  "sku": "SK8772",
                  "skuName": "Domestic and International Calling Plan",
                  "description": "For Businesses that need to enable online users to place or receive Domestic and International calls through the Public Switched Telephone Network (PSTN). There are base pre-requisites required to purchase this offering.",
                  "zeroValueSku": "t",
                  "qtyMin": "1",
                  "qtyMax": "",
                  "vendorMapId": "ded34535-507f-4246-8370-f9180318c537",
                  "manufacturerPartNumber": "ded34535507f42468370f9180318c537",
                  "article": "3968760"
                },
              }
              ]
            }
          ]
        }
      ]
    }
  ]
}
},
  "Key": 3298012
}


    public class AddOn
     {
    [Newtonsoft.Json.JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public string vendorMapId { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }
    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
}

public class Sku
{
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }
    public string vendorMapId { get; set; }
    public string billingType { get; set; }
    public string productType { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public List<AddOn> addOns { get; set; }
    public List<string> datacenterLocations { get; set; }
    public List<AddOn2> add { get; set; }
}

public class AddOn2
{
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }

    public List<Sku> sk { get; set; }
}

public class Listing
{
    public string listingName { get; set; }
    public List<Sku> skus { get; set; }
    public List<AddOn2> addOns { get; set; }

    //new
    //public List<AddOn2> addon2s { get; set; }
}

public class Vendor
{
    public string vendorId { get; set; }
    public string vendorName { get; set; }//microsoft
    public List<Listing> listings { get; set; }

   // public List<Sku> skus { get; set; }
}

public class Products
{
    public int totalProducts { get; set; }
    public int recordsPerPage { get; set; }
    public int page { get; set; }
    public int totalPages { get; set; }
    public List<Vendor> vendors { get; set; }
}

public class BodyText
{
    public Products products { get; set; }
}

public class RootObject
{
    public string Result { get; set; }
    public BodyText BodyText { get; set; }
    public int Key { get; set; }
}
  }



   RootObject skuIdName = JsonConvert.DeserializeObject<RootObject>(jsonStorageProducts);

3 个答案:

答案 0 :(得分:2)

不确定您的意思是

void Main()
{
    string jsonStorageProducts = @"
{
   ""Result"": ""Success"",
   ""BodyText"": 
  {
  ""products"": 
{
  ""totalProducts"": 510,
  ""recordsPerPage"": 10,
  ""page"": 1,
  ""totalPages"": 51,
  ""vendors"": [
    {
      ""vendorId"": ""397"",
      ""vendorName"": ""Microsoft"",
      ""listings"": [
        {
          ""listingName"": ""Office 365 Enterprise"",
          ""skus"": [
            {
              ""sku"": ""SK10228"",
              ""skuName"": ""Microsoft 365 E5 without Audio Conferencing"",
              ""description"": ""Pending"",
              ""zeroValueSku"": ""t"",
              ""manufacturerPartNumber"": ""db5e0b1c9cc3459c9d08c61993959fd3"",
              ""article"": ""4090153"",
              ""vendorMapId"": ""db5e0b1c-9cc3-459c-9d08-c61993959fd3"",
              ""billingType"": ""Monthly"",
              ""productType"": ""SaaS"",
              ""qtyMin"": ""1"",
              ""qtyMax"": """",
              ""addOns"": [
                {
                  ""sku"": ""SK8265"",
                  ""skuName"": ""Audio Conferencing"",
                  ""description"": ""For businesses that need to enable users to dial-in a number to join Skype meetings, or dial-out to bring participants into the meeting. There are base pre-requisites required to purchase this offering."",
                  ""zeroValueSku"": ""t"",
                  ""qtyMin"": ""1"",
                  ""qtyMax"": """",
                  ""vendorMapId"": ""c94271d8-b431-4a25-a3c5-a57737a1c909"",
                  ""manufacturerPartNumber"": ""c94271d8b4314a25a3c5a57737a1c909"",
                  ""article"": ""3873033""
                },
                {
                  ""sku"": ""SK8772"",
                  ""skuName"": ""Domestic and International Calling Plan"",
                  ""description"": ""For Businesses that need to enable online users to place or receive Domestic and International calls through the Public Switched Telephone Network (PSTN). There are base pre-requisites required to purchase this offering."",
                  ""zeroValueSku"": ""t"",
                  ""qtyMin"": ""1"",
                  ""qtyMax"": """",
                  ""vendorMapId"": ""ded34535-507f-4246-8370-f9180318c537"",
                  ""manufacturerPartNumber"": ""ded34535507f42468370f9180318c537"",
                  ""article"": ""3968760""
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}
},
  ""Key"": 3298012
}
";
    RootObject skuIdName = JsonConvert.DeserializeObject<RootObject>(jsonStorageProducts);
    var productSku = skuIdName.BodyText.products.vendors
     .SelectMany(x =>x.listings
     .SelectMany(l => l.skus
     .SelectMany(s => s.addOns
     .Select(o => new { 
        SKU = o.sku, 
        Name = o.skuName }))));

    foreach (var sku in productSku)
    {
        Console.WriteLine($"SKU: {sku.SKU}, Name: {sku.Name}");
    }
}


public class AddOn
{
    [Newtonsoft.Json.JsonProperty(PropertyName = "id")]
    public string Id { get; set; }
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public string vendorMapId { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }
    public override string ToString()
    {
        return JsonConvert.SerializeObject(this);
    }
}

public class Sku
{
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }
    public string vendorMapId { get; set; }
    public string billingType { get; set; }
    public string productType { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public List<AddOn> addOns { get; set; }
    public List<string> datacenterLocations { get; set; }
    public List<AddOn2> add { get; set; }
}

public class AddOn2
{
    public string sku { get; set; }
    public string skuName { get; set; }
    public string description { get; set; }
    public string zeroValueSku { get; set; }
    public string qtyMin { get; set; }
    public string qtyMax { get; set; }
    public string manufacturerPartNumber { get; set; }
    public string article { get; set; }

    public List<Sku> sk { get; set; }
}

public class Listing
{
    public string listingName { get; set; }
    public List<Sku> skus { get; set; }
    public List<AddOn2> addOns { get; set; }

    //new
    //public List<AddOn2> addon2s { get; set; }
}

public class Vendor
{
    public string vendorId { get; set; }
    public string vendorName { get; set; }//microsoft
    public List<Listing> listings { get; set; }

    // public List<Sku> skus { get; set; }
}

public class Products
{
    public int totalProducts { get; set; }
    public int recordsPerPage { get; set; }
    public int page { get; set; }
    public int totalPages { get; set; }
    public List<Vendor> vendors { get; set; }
}

public class BodyText
{
    public Products products { get; set; }
}

public class RootObject
{
    public string Result { get; set; }
    public BodyText BodyText { get; set; }
    public int Key { get; set; }
}

输出:

SKU: SK8265, Name: Audio Conferencing
SKU: SK8772, Name: Domestic and International Calling Plan

答案 1 :(得分:0)

添加可能有助于您获取所需值的lambda表达式。下面是获取SKU列表的示例。

var root = new RootObject();
var skus = new List<Sku>();
root.BodyText.products.vendors.ForEach(vendor =>
        vendor.listings.ForEach(listing => listing.skus.ForEach(sku => skus.Add(sku))));

上面的代码将为您提供所有清单的所有供应商的SKU,依此类推。现在以这个为例,您应该可以继续进行。如果您需要进一步的帮助,请告诉我。

答案 2 :(得分:0)

您可以使用positionOffset中的ForEach遍历模型并按如下方式填充字典:

LINQ

最终,您应该在字典中获得var nameIdDict = new Dictionary<string, string>(); skuIdName.BodyText.products.vendors.ForEach(v => { v.Listings.ForEach(listing => { listing.skus.ForEach(s => { nameIdDict.Add(s.sku, s.skuName); s.addOns.ForEach(a => { nameIdDict.Add(a.sku, a.skuName); }); s.add.ForEach(a => { nameIdDict.Add(a.sku, a.skuName); }); }); listing.addOns.ForEach(a => { nameIdDict.Add(a.sku, a.skuName); }); }); }); 和相应的sku。虽然您不知道它在json的哪个级别上存在。