22 September 2017

Extending HtmlHelper

Helpers.cs
 using System.Web;  
 using System.Web.Mvc;  
 using System.Web.Routing;  
 namespace mvc.Helpers  
 {  
   public static class ImageHelpers  
   {  
     public static IHtmlString Image(this HtmlHelper helper, string id, string url, string alternateText)  
     {  
       return Image(helper, id, url, alternateText, null);  
     }  
     public static IHtmlString Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes)  
     {  
       var builder = new TagBuilder("img");  
       builder.GenerateId(id);  
       builder.MergeAttribute("src", url);  
       builder.MergeAttribute("alt", alternateText);  
       builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));  
       return new MvcHtmlString(builder.ToString(TagRenderMode.SelfClosing));  
     }  
   }  
 }  
index.cshtml
 @{  
   ViewBag.Title = "Home Page";  
 }  
 @using mvc.Helpers;  
 @Html.Image("img1", "~/Content/XBox.jpg", "XBox Console")  

04 September 2017

Array mapping using LINQ

 namespace ConsoleApplication1  
 {  
   public class Student  
   {  
     public string FirstName;  
     public string LastName;  
   }  
   public class StudentFullName  
   {  
     public string FullName;  
     public StudentFullName(Student student)  
     {  
       FullName = student.FirstName + " " + student.LastName;  
     }  
     public override string ToString()  
     {  
       return FullName;  
     }  
   }  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       Student s1 = new Student { FirstName = "Jack", LastName = "Frost" };  
       Student s2 = new Student { FirstName = "Mary", LastName = "Wong" };  
       Student s3 = new Student { FirstName = "Ronald", LastName = "McDonalds" };  
       Student s4 = new Student { FirstName = "John", LastName = "Smith" };  
       List<Student> studentArray = new List<Student>() { s1, s2, s3, s4 };  
       List<StudentFullName> fullNameArray = studentArray.Select(a => new StudentFullName(a)).ToList();  
       string nameList = fullNameArray.OrderBy(m => m.FullName).Select(m => m.FullName).Aggregate((current, next) => current + ", " + next);  
       Console.WriteLine(nameList);  
       Console.ReadLine();  
     }  
   }  
 }  

Concatenation using LINQ

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Linq.Expressions;  
 namespace ConsoleApplication1  
 {  
   public class Student  
   {  
     public string FirstName;  
     public string LastName;  
   }  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       Student s1 = new Student { FirstName = "Jack", LastName = "Frost" };  
       Student s2 = new Student { FirstName = "Mary", LastName = "Wong" };  
       Student s3 = new Student { FirstName = "Ronald", LastName = "McDonalds" };  
       Student s4 = new Student { FirstName = "John", LastName = "Smith" };  
       List<Student> studentArray = new List<Student>() { s1, s2, s3, s4 };  
       string nameList = studentArray.OrderBy(m => m.LastName).Select(m => m.LastName).Aggregate((current, next) => current + ", " + next);  
       Console.WriteLine(nameList);  
       Console.ReadLine();  
     }  
   }  
 }