How to combine two flat lists into one nested object

后端 未结 4 533
-上瘾入骨i
-上瘾入骨i 2021-01-25 12:25

I have three lists which contains three same properties in each collection. I want to combine a result into one collection. Ex classes structure is as below

publ         


        
4条回答
  •  醉酒成梦
    2021-01-25 12:57

    There seems to be a number of questions here combined, I'll try to work through them:

    Data Models and Deserialization

    With respect to generating a single structure from your API response, I would recommend using the Newtonsoft.Json libraries, available on NuGet Json.NET. They will allow you to deserialize the response from your API, into a single object, which given the sample you provided, should contain a collection of each of your models, Order, PaymentDetail, CouponUsageDetail:

    public class APIResponceContainer
    {
        [JsonProperty("Orders")]
        public List Orders { get; set; }
        [JsonProperty("PaymentDetails")]
        public List PaymentDetails { get; set; }
        [JsonProperty("CouponUsageDetails")]
        public List CouponUsageDetails { get; set; }
    
        public APIResponceContainer()
        {
            Orders = new List();
            PaymentDetails = new List();
            CouponUsageDetails = new List();
        }
    }
    

    Be aware to add the required attributes to each of your models as so:

    public class Order
    {
        [JsonProperty("ProductId")]
        public int ProductId { get; set; }
        [JsonProperty("CustomerId")]
        public int CustomerId { get; set; }
        [JsonProperty("OrderId")]
        public int OrderId { get; set; }
    }
    

    Deserialization then happens from your JSON string, as such:

    StringReader stringReader = new StringReader(myJSONString);
    JsonSerializer js = JsonSerializer.Create();
    APIResponceContainer APIResponce = (APIResponceContainer)js.Deserialize(stringReader, typeof(APIResponceContainer));
    

    Queries

    As discussed in the comments, your data is unfortunately in terrible need of normalization. However, what I have inferred is that you would like to produce a flat structure, maintaining the "Few other Properties" and "key properties", for a combination of Order, PaymentDetail and CouponUsageDetail. You can use Linq for this, importantly I would recommend you choose yourself a "Primary Key". In other words, one property that can independently tie all the others together. In the example below, I have choose OrderID since it should be unique (?):

    var flatSequence =
                from order in APIResponce.Orders
                join coupon in APIResponce.CouponUsageDetails on order.OrderId equals coupon.OrderId
                join payment in APIResponce.PaymentDetails on order.OrderId equals payment.OrderId
                select new
                {
                    // Here extract all the properties you care about
                    OrderID = order.OrderId,
                    Customer = order.CustomerId,
                    Product = order.ProductId,
                    // All the "other Properties" ?
                    BankDetail = payment.PaymentOnlyProperty
                };
    

    Here I have extracted to var, however if you know the final flat structure you would like, of course determine a class of your own to receive the result.

    Please comment if there is any questions.

提交回复
热议问题