Web API Edition | ComponentOne
Web API / Working with Web API / Configuring Web API / Configuring .NET Collections
In This Topic
    Configuring .NET Collections
    In This Topic

    Data in .NET collections can also be used to generate, merge, and convert excel in a desired format. This section demonstrates how to configure .NET collection in Web API service application. The example implements IEnumerable interface to use data generated in models.

    Note: Once you have configured .NET Collections in Web API service application, you can create client side application to call the services to use the .NET Collections. For more information,  see Web API Services topic.
    1. Create desired model classes within Models folder in your service application. For example, here we create Sales.cs and CustomerOrder.cs models.
      Example Title
      Copy Code
      public class Sale
          {        
              public int ID { get; set; }
              public DateTime Start { get; set; }
              public DateTime End { get; set; }
              public string Country { get; set; }
              public string Product { get; set; }
              public string Color { get; set; }
              public double Amount { get; set; }
              public double Amount2 { get; set; }
              public double Discount { get; set; }
              public bool Active { get; set; }
      
              public MonthData[] Trends { get; set; }
              public int Rank { get; set; }
              public static IEnumerable <Sale> GetData(int total)
              {
                  var countries = new[] { "US", "UK", "Canada", "Japan", "China", "France", "German", "Italy", "Korea", "Australia" };
                  var products = new[] { "Widget", "Gadget", "Doohickey" };
                  var colors = new[] { "Black", "White", "Red", "Green", "Blue" };
                  var rand = new Random(0);
                  var dt = DateTime.Now;
                  var list = Enumerable.Range(0, total).Select(i =>
                  {
                      var country = countries[rand.Next(0, countries.Length - 1)];
                      var product = products[rand.Next(0, products.Length - 1)];
                      var color = colors[rand.Next(0, colors.Length - 1)];
                      var date = new DateTime(dt.Year, i % 12 + 1, 25, i % 24, i % 60, i % 60);
      
                      return new Sale
                      {
                          ID = i + 1,
                          Start = date,
                          End = date,
                          Country = country,
                          Product = product,
                          Color = color,
                          Amount = rand.NextDouble() * 10000 - 5000,
                          Amount2 = rand.NextDouble() * 10000 - 5000,
                          Discount = rand.NextDouble() / 4,
                          Active = (i % 4 == 0),
                          Trends = Enumerable.Range(0, 12).Select(x => new MonthData { Month = x + 1, Data = rand.Next(0, 100) }).ToArray(),
                          Rank = rand.Next(1, 6)
                      };
                  });
                  return list;
              }
      
              internal static dynamic GetCountries()
              {
                  throw new NotImplementedException();
              }
      
              internal static dynamic GetProducts()
              {
                  throw new NotImplementedException();
              }
          }
      
          public class MonthData
          {
              public int Month { get; set; }
              public double Data { get; set; }
          }
      }
      
      C#
      Copy Code
      public partial class FlexGridController: Controller {
        public ActionResult Index() {
          var customers = db.Customers.Take(10).ToList();
          var model = customers.Select(c => new CustomerWithOrders {
            CustomerID = c.CustomerID,
              CompanyName = c.CompanyName,
              Country = c.Country,
              City = c.City,
              Orders = (db.Orders.Where(o => o.CustomerID == c.CustomerID).ToList())
          });
          return View(model);
        }
      }                                                       
      
    2. Configure the models within Configuration (IAppBuilder app) method of Startup class. This is achieved by adding dataset in the Data Provider Manager of your service application through AddItemsSource() method, as shown below.
             
      C#
      Copy Code
       app.UseDataProviders().AddItemsSource("Sales", () => Sale.GetData(10).ToList()).AddItemsSource("Orders", () => CustomerOrder.GetOrderData(20).ToList())