Posts

Showing posts from January, 2017

Excel file from Controller

Very easy way to create Excel quickly.... public void ExcelResults(int id) {          Business.Agreement_Provider agr = new Business.Agreement_Provider();          List<MyModel> model = new List<MyModel>();          var agreements = agr.getExpiringAgreements(id);          model = agreements.ToList();          model.OrderBy(p => p.My_Number);          Export export = new Export();          export.ToExcel(Response, model);         } Two methods  - Call the first one from the page and then it uses the public Export class to return a file. The nice thing about the Export class is that we are passing our model in as an object   - this allows us to reuse this same class for any model. public class Export         {             public void ToExcel(HttpResponseBase Response, object myList)             {                 var grid = new System.Web.UI.WebControls.GridView();                 var thisDate = DateTime.Now.ToShortDateString().Replace("/"

Linq list with counts

 Return a list with item counts such as Apples - 3 Oranges -7 Berries - 15 public List<SelectListItem> MyFruityList() { //Declare a new list of items List<SelectListItem> items = new List<SelectListItem>();  int totalcount = 0; //get all your objects and add to a variable var allfruits= prog.getallFruits(); //This next method returns a FruitId, FruitName and FruitCount var pg = prog.getExpiringFruitCounts().OrderByDescending(p => p.FruitCount); var alljoined = (from a in  allfruits                                  join c in pg     on new { a.FruitId, a.FruitName } equals new { c.FruitId, c.FruitName } into joinedData     f rom c in joinedData.DefaultIfEmpty()  group a by new { a,c } into ord                                                                  select new SpecialFruit_DTO                                  {                                      Fruitid = ord.Key.a.FruitId,                                      FruitName =ord.Key.a.

Meta Data - Annotating Models

One of the issues with using Entity Framework is that you may want some special annotation but you don't want to use a DTO or other object outside of EF. One way to afford you the opportunity to Annotate on the original EF model is to extend with a partial class of meta data. In your Data Models folder (or really anywhere in your data project) you would add a class (Partial Classes) that would contain empty pointers to your data classes - (in my tt files I have a company class and a systemUser class which represent models from the tables in my database): PartialClasses.cs using System.ComponentModel.DataAnnotations; namespace MySite.Data {     [MetadataType(typeof(systemUsersMetadata))]     public partial class systemUser     {     }     [MetadataType(typeof(companyMetadata))]     public partial class company     {     } } Once you have this setup you can begin to annotate! Create a systemUsersMetadata.cs class and you can now add annotations while still using yo