Linq

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday 31 August 2016

Linq Query Can be Executed More than once

A linq Query can be executed more than once.

Source Code
using System.Linq;  
 using System;  
 namespace GroupJoin  
 {  
   class Program  
   {  
     public static void Main()  
     {  
       int[] number = { 101, 102, 103, 104, 105 };  
       var final = from num in number  
             where num > 102  
             select num;  
       foreach (var item in final)  
       {  
         Console.WriteLine(item);  
       }  
       number[1] = 200;  
       Console.WriteLine("----after---");  
       foreach (var item in final)  
       {  
         Console.WriteLine(item);  
       }  
       Console.ReadLine();  
     }  
   }  
 }  

Output










Debugging











Monday 11 July 2016

Linq :- Some of the handy statements

  • These are some of the common interview question asked.
1)Remove Duplicates from a list
using System.Linq;  
 using System;  
 using System.Collections.Generic;  
 namespace GroupJoin  
 {  
   class Program  
   {  
     public static void Main()  
     {  
       List<string> list = new List<string>() { "anurag","anurag","abc","abc","xyz"};  
       Console.WriteLine("::Duplicate Strings in a List::");  
       var duplicateString = list.GroupBy(x => x)  
             .Where(group => group.Count() > 1)  
             .Select(group => group.Key);  
       foreach (var item in duplicateString)  
       {  
         Console.WriteLine(item);  
       }  
       Console.ReadLine();  
     }  
   }  
 }  


2)Remove duplicates in a given string
using System.Linq;  
 using System;  
 using System.Collections.Generic;  
 using System.Text.RegularExpressions;  
 namespace GroupJoin  
 {  
   class Program  
   {  
     public static void Main()  
     {  
       string input = "anurag;anurag;anurag2;anurag3;anurag4";  
       string output = string.Join(";", input.Split(';').Distinct().ToArray());  
       Console.WriteLine(output);  
       Console.ReadLine();  
     }  
   }  
 }  


3)Finding Smallest String
 using System.Linq;  
 using System;  
 using System.Collections.Generic;  
 namespace GroupJoin  
 {  
   class Program  
   {  
     public static void Main()  
     {  
       List<string> list = new List<string>() { "anurag","abhishek","xyz"};  
       Console.Write("Small String-> ");  
       Console.WriteLine(list.OrderByDescending(x => x.Length).Last());  
       Console.ReadLine();  
     }  
   }  
 }  






4)Remove duplicate characters of a string
using System.Linq;  
 using System;  
 using System.Collections.Generic;  
 namespace GroupJoin  
 {  
   class Program  
   {  
     public static void Main()  
     {  
       string s = "Dddduppplicaaate";   
       Console.Write("Removed Duplicate Characters -> ");  
       Console.WriteLine(new string(s.ToLower().ToCharArray().Distinct().ToArray()));  
       Console.ReadLine();  
     }  
   }  
 }  







5)Occurrences of string
 using System.Linq;  
 using System;  
 using System.Collections.Generic;  
 using System.Text.RegularExpressions;  
 namespace GroupJoin  
 {  
   class Program  
   {  
     public static void Main()  
     {  
       string s = "This is great.But is this really great ??";  
       Console.Write("String Occurences(this) -> ");  
       Console.WriteLine(Regex.Matches(s, "this", RegexOptions.IgnoreCase).Count);  
       Console.ReadLine();  
     }  
   }  
 } 







True For All and ALL(Check if all values are same)

using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 namespace LINQ  
 {  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       List<int> num1 = new List<int> { 2, 2, 2, 2 };  
       bool evenOrOdd = num1.TrueForAll(x => x == 2);  
       Console.WriteLine(evenOrOdd);  
       Console.ReadLine();  
     }  
   }  
 } 

Output









This gives the Same output:

 bool evenOrOdd= num1.All(x=>x==2);

Will update more on this post :)












Sunday 26 June 2016

Interview Question:- How to use left join in Linq ?

Scenario

  • List of Employee. 
  • List of Departments.
  • Each Employee belongs to Particular department
  • There are some Employees which belongs to some departments which are not there in the department list.
  • Need all the Employees and the department name. Mark 'No Department' for those employees whose departmentId doesn't match with department list.


Code
 using System;    
  using System.Collections.Generic;    
  using System.Linq;    
  using System.Text;    
  namespace GroupJoin    
  {    
  class Program    
  {    
   public static void Main()    
   {    
   List<Employee> objEmployeelist =  new List<Employee>        
    {        
    new Employee{ EmployeeID=1, EmployeeName ="Anurag", DepID=1},    
    new Employee{ EmployeeID=2, EmployeeName ="Abhishek",DepID=2},    
    new Employee{ EmployeeID=4, EmployeeName ="Ayush", DepID=1},    
    new Employee{ EmployeeID=3, EmployeeName ="Rahul", DepID=6}    
    };    
     List<Department> objDepartmentlist = new List<Department>     
    {    
    new Department {DepartmentID=1,DepartmentName="HR"},    
    new Department {DepartmentID=2,DepartmentName="Admin"},    
    new Department {DepartmentID=3,DepartmentName="Lib"},    
    new Department {DepartmentID=4,DepartmentName="Abc"}    
    };    
     var list = (from e in objEmployeelist    
     join d in objDepartmentlist    
     on e.DepID equals d.DepartmentID into ds    
     from d in ds.DefaultIfEmpty()    
     select new { emp = e, dep=(d==null ?"No Department":d.DepartmentName) }).ToList();    
    // loop over here == null ? "(No products)" : p.ProductName    
    foreach (var item in list)    
    {    
    Console.WriteLine(item.emp.EmployeeName + "::" + item.dep);    
    }    
    Console.ReadLine();    
   }    
   }    
   public class Employee    
   {    
   public int EmployeeID;    
   public string EmployeeName;    
   public int DepID;    
   }    
   public class Department    
   {    
   public int DepartmentID;    
   public string DepartmentName;    
   }    
  }    

Output


Saturday 14 May 2016

Inner Join linq c#


Inner Join using Lambda Expression and Linq

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace GroupJoin  
 {  
   class Program  
   {  
     public static void Main()  
     {  
       List<Employee> objEmployeelist =  
       new List<Employee>   
       {                  
       new Employee{ EmployeeID=1, EmployeeName ="Anurag", DepID=1},              
       new Employee{ EmployeeID=2, EmployeeName ="Abhishek",DepID=2},                  
       new Employee{ EmployeeID=3, EmployeeName ="Rahul", DepID=6}  
       };  
       List<Department> objDepartmentlist = new List<Department>  
       {  
        new Department {DepartmentID=1,DepartmentName="HR"},  
        new Department {DepartmentID=2,DepartmentName="Admin"},  
        new Department {DepartmentID=3,DepartmentName="Lib"},  
        new Department {DepartmentID=4,DepartmentName="Abc"}  
       };  
       var listlinq = from e in objEmployeelist  
               join d in objDepartmentlist  
               on e.DepID equals d.DepartmentID  
               select new { emp = e, dep = d };  
       var listlambda = objEmployeelist.Join(objDepartmentlist, e => e.DepID, d => d.DepartmentID,  
         (e, d) => new { emp = e, dep = d });  
       foreach (var item in listlambda)  
       {  
         Console.WriteLine(item.emp.EmployeeName + "::" + item.dep.DepartmentName);  
       }  
       Console.ReadLine();  
     }  
   }  
   public class Employee  
   {  
     public int EmployeeID;  
     public string EmployeeName;  
     public int DepID;  
   }  
   public class Department  
   {  
     public int DepartmentID;  
     public string DepartmentName;  
   }  
 }