Posts

Showing posts from October, 2016

A prettier way to use Enums

I think we have all struggled with using Enums with Linq, most especially when trying to plug them into your results.  A far better approach is to decorate your classes with them.  Let me explain We start by declaring a typical Enum class - something like this.   public enum OrderStatus     {         /// <summary>         /// Pending         /// </summary>         Pending = 10,         /// <summary>         /// Processing         /// </summary>         Processing = 20,         /// <summary>         /// Complete         /// </summary>         Complete = 30,         /// <summary>         /// Cancelled         /// </summary>         Cancelled = 40     } Then when we want to take advantage of it - in our Orders class we declare a string of OrderStatus - keep in mind our database only has OrderStatusId.  Here is what this property looks like.  public OrderStatus OrderStatus         {             get             {

Windows Authentication, Custom Roles, MVC 5

There are no doubt other ways to accomplish Role Management while still keeping Windows authentication but the way I am showing you here has to be one of the easier implementation. Let's start with the config.  After the authentication we can add in the roleManager reference.  The 'defaultProvider' is the name of our file for our CustomRoleProvider .   <system.web> ...   <authentication mode="Windows"/>     <authorization>       <deny users="?"/>     </authorization>     <roleManager enabled="true" defaultProvider="CustomRoleProvider">       <providers>         <clear />         <add name="CustomRoleProvider" type="MyNameSpace.CustomRoleProvider"/>       </providers>     </roleManager>   </system.web> Once we have the config setup we will need to add in two files Authorization file which will inherit from AuthorizeAttribute

Comparing Strings in Linq

I like this method best var customer = db.Customers.Where(d => string.Compare(d.Email,email,true)==0).FirstOrDefault(); It replaces the ToUpper() comparison and is nice and clean

Case Statement inside LINQ

Example of a mock Case Statement inside LINQ wrapped inside the parens.  var territoryInfo= (from e in  Franchisee_Zips                             where e.AccountCode == accountcode                             select new  Data.FullTerritory                             {                                 AccountCode = e.AccountCode,                                 VendorName = e.VendorName,                                 Territory_Type = (e.TerritoryType == 0 ? "TAFS" :                                 e.TerritoryType == 1 ? "Purchased Territory" :                                 e.TerritoryType == 3 ? "Option" :                                 e.TerritoryType == 4 ? "Vendor" : "Unknown"                                 ),                                 OptionBegins = e.OptionBegins,                                 OptionExpires = e.OptionExpires,                                 Action = "Add"