Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Union or concat 2 data collections for a single web api output

$
0
0

Hello I have 2 separate controllers providing a json file that is essentially the same structure. I would ideally like to consolidate the 2 api calls into 1, outputting a single json file.

 I am unsure of how or the best / easiest way to do this.

[  
   {  "Category":"WO","data":[  
         {  "ID":43,"WONumber":"WO10018","SortOrder":43
         }
      ]
   }
]and[  
   {  "Category":"Milestone Health","data":[  
         {  "ID":14752,"cfg_item":"Green","SortOrder":0
         },
         {  "ID":14753,"cfg_item":"Amber","SortOrder":1
         },
         {  "ID":14754,"cfg_item":"Red","SortOrder":3
         }
      ]
   },
   {  "Category":"Milestone Status","data":[  
         {  "ID":218,"cfg_item":"Ready","SortOrder":0
         },
         {  "ID":23,"cfg_item":"Open","SortOrder":1
         },
         {  "ID":24,"cfg_item":"Complete","SortOrder":2
         }
      ]
   }
] 

My data model is as follows

namespace Config.Models
{

    public class ConfigItem
    {
        public int ID { get; set; }
        public string cfg_item { get; set; }
        public Nullable<int> SortOrder { get; set; }
    }

    public class ViewConfigItems
    {
        public string Category { get; set; }
        public IEnumerable<ConfigItem> data { get; set; }

    }

    public class WOItem
    {
        public int ID { get; set; }
        public string WONumber { get; set; }
        public Nullable<int> SortOrder { get { return ID; } }
    }

    public class ViewWOItems
    {
        public string Category { get; set; }
        public IEnumerable<WOItem> data { get; set; }

    }

    // this is my attempt to combine the data models (note above WONumber and cfg_item are where the models differ slightly) I know this is not right, but not sure what to do
    public class ViewCombinedItems
    {
        public IEnumerable<ViewConfigItems> cdata { get; set; }
        public IEnumerable<ViewWOItems> wdata { get; set; }
    }
}

my attempt at a controller

using Config.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Web.Http.Description;


[Authorize]
public class TESTController : ApiController
{

    private OneView_PPM db = new OneView_PPM();


    public IEnumerable<ViewCombinedItems> GetCombinedItems(string id, int pno, string part)
    {
        var WOItems = from f in db.WOItems
                          where f.ProjectRef == pno && f.PartitNo == part
                          //orderby f.ID ascending
                          select f;

        var ConfigItems = from e in db.ConfigItems
                          where e.FormName == id && e.PartitNo == part
                          //orderby e.Category ascending, e.SortOrder ascending
                          select e;

        // ------------------- This is where I get lost - completely ------------------------------------
      //  var baselist = WOItems.Cast<ViewCombinedItems>().Union<ViewCombinedItems>(ConfigItems.Cast<ViewCombinedItems>());
        // Tried this first not working either -----------------------------------------
        var list2 = WOItems.Cast<ViewCombinedItems>();
        var list1 = ConfigItems.Cast<ViewCombinedItems>();        
        var baselist = list1.Concat(list2); //optional .ToList()
        var dresult = baselist.GroupBy(c => c.Category).Select(g => new ViewCombinedItems()
        {
            Category = g.Key,
            data = g.ToList().Select(c => new ViewCombinedItems()
            {
                ID = c.ID,
                WONumber = c.WONumber,  // unsure how to handle WONumber vrs cfg_itm  (in sql id just use an alias  eg WONumber as cfg_item) 
                SortOrder = c.SortOrder
            })
        });
        return dresult;       
    }
}

Hopefully someone can point me in the right direction - as I'm a little lost, and not even sure if my approach is correct

Thanks in advance...

r


Viewing all articles
Browse latest Browse all 4850


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>