Linq

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

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;  
   }  
 }  









Sunday, 13 September 2015

SelectMany In Linq c#


SelectMany

  • Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.
  • Projection Operator like Select
  • When we nested list, SelectMany becomes useful. We need only one loop using selectMany to traverse elements of the nested list.
Scenario
  • List of students and have list of subjects in it.
  • Find out the subjects which Mike is enrolled to ?
Code
using System;  
 using System.Collections.Generic;  
 using System.Globalization;  
 using System.Linq;  
 using System.Text;  
 using System.Threading.Tasks;  
 namespace ConsoleApplication1  
 {  
   class Program  
   {  
     class SoftwareStudent  
     {  
       public string Name { get; set; }  
       public string RollNumber { get; set; }  
       public List<string> Subject { get; set; }  
     }  
     static void Main(string[] args)  
     {  
       List<SoftwareStudent> students = new List<SoftwareStudent>{  
                    new SoftwareStudent {  
                      Name = "Anurag",  
                      RollNumber ="1" ,  
                      Subject= new List<string>{"Dotnet","Java","Informatica"}},  
                    new SoftwareStudent {  
                      Name = "Abhishek",  
                      RollNumber ="2" ,  
                      Subject= new List<string>{"Che","Phy"}},  
                    new SoftwareStudent {  
                      Name = "Mike",  
                      RollNumber ="3" ,  
                      Subject= new List<string>{"Dotnet","php","BigData"}}};  
       var result4 = students.Where(x => x.Name == "Mike").ToList();  
       string subjectMike=string.Empty;  
       foreach (var item in result4)  
       {  
         var count = item.Subject.Count;  
         foreach (var item1 in item.Subject)  
         {  
           subjectMike += item1 + ";";  
           if (--count <= 0)  
           {  
             subjectMike = subjectMike.TrimEnd(';');//Dotnet;php;BigData  
           }   
         }  
       }  
        Console.WriteLine(subjectMike);  
        //*******************SELECT MANY*****************//  
       string subjectMike1 = string.Empty;  
       var MikeSubSelMany = students.Where(x => x.Name == "Mike").SelectMany(student => student.Subject, (st1, subject) => new { st1.RollNumber,st1.Name, subject });  
       //var MikeSubSelMany1 = students.Where(x => x.Name == "Mike").SelectMany(student => student.Subject);  
       var count2 = MikeSubSelMany.Count();  
       foreach (var item2 in MikeSubSelMany)  
       {  
         subjectMike1 += item2.subject + ";";  
         if (--count2 <= 0)  
         {  
           subjectMike1 = subjectMike.TrimEnd(';');//Dotnet;php;BigData  
         }   
       }  
       Console.WriteLine(subjectMike1);  
       Console.ReadLine();  
     }  
   }  
 }  


So highlighted is the use of selectmany. You only need to loop once to get the results of the subjects taken by mike.

This Piece of code also produces the same result.

Using overloaded version of SelectMany

 var MikeSubSelMany = students.Where(x => x.Name == "Mike").  
       SelectMany(student => student.Subject);   
     //var MikeSubSelMany1 = students.Where(x => x.Name == "Mike").SelectMany(student => student.Subject);   
     var count2 = MikeSubSelMany.Count();   
     foreach (var item2 in MikeSubSelMany)   
     {   
      subjectMike1 += item2 + ";";   
      if (--count2 <= 0)   
      {   
       subjectMike1 = subjectMike.TrimEnd(';');//Dotnet;php;BigData   
      }    
     }   
     Console.WriteLine(subjectMike1);   
     Console.ReadLine(); 


Debugging


O/P with with both SelectMany and without Selectmany gives same result







Tuesday, 16 December 2014

GroupBy & Join Linq C#

Group By Linq
  • powerful feature of linq Similar to Group by as SQL
  • It can be done using both linq as well as lambda expression.
Code
using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 namespace XML  
 {  
   class Program  
   {  
     public class student  
     {  
       public int id { get; set; }  
       public string name { get; set; }  
       public int classId { get; set; }  
       public List<student> getStudents()  
       {  
         List<student> obj = new List<student>();  
         obj.Add(new student { id = 101, name = "abc1", classId = 1 });  
         obj.Add(new student { id = 102, name = "abc2", classId = 1 });  
         obj.Add(new student { id = 103, name = "abc3", classId = 2 });  
         obj.Add(new student { id = 104, name = "abc4", classId = 2 });  
         obj.Add(new student { id = 105, name = "abc5", classId = 2 });  
         obj.Add(new student { id = 106, name = "abc6", classId = 3 });  
         obj.Add(new student { id = 107, name = "abc7", classId = 3 });  
         obj.Add(new student { id = 108, name = "abc8", classId = 3 });  
         obj.Add(new student { id = 109, name = "abc9", classId = 3 });  
         return obj;  
       }  
     }  
     static void Main(string[] args)  
     {  
       student s = new student();  
       var students = s.getStudents().ToList();  
       var final = students.GroupBy(x => x.classId).ToDictionary(y => y.Key, y => y.ToList());  
       foreach (var x in final)  
       {  
         Console.WriteLine("----" + x.Key + "-----");  
         foreach (var item in x.Value)  
         {  
           Console.WriteLine(item.id);  
           Console.WriteLine(item.name);  
           Console.WriteLine();  
         }  
         Console.WriteLine("===================================");  
       }  
       Console.WriteLine("--------Slight Modification----------");  
       var final1 = students.GroupBy(x => x.classId).Select(g => new { Key=g.Key, List=g.ToList() });  
       foreach (var item in final1)  
       {  
         Console.WriteLine("----" + item.Key + "-----");  
         foreach (var item1 in item.List)  
         {  
           Console.WriteLine(item1.id);  
           Console.WriteLine(item1.name);  
           Console.WriteLine();  
         }  
       }  
       Console.ReadLine();  
     }  
   }  
 }  

Both Gives Same result: 



























Group By and Sum

Code
using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 namespace LINQ  
 {  
   class Program  
   {  
     public class student  
     {  
       public int id { get; set; }  
       public string name { get; set; }  
       public int classId { get; set; }  
       public int Salary { get; set; }  
       public List<student> getStudents()  
       {  
         List<student> obj = new List<student>();  
         obj.Add(new student { id = 101, name = "abc1", classId = 1, Salary = 100 });  
         obj.Add(new student { id = 102, name = "abc2", classId = 1, Salary = 200 });  
         obj.Add(new student { id = 103, name = "abc3", classId = 2, Salary = 300 });  
         obj.Add(new student { id = 104, name = "abc4", classId = 2, Salary = 800 });  
         obj.Add(new student { id = 105, name = "abc5", classId = 2, Salary = 900 });  
         obj.Add(new student { id = 106, name = "abc6", classId = 3, Salary = 250 });  
         obj.Add(new student { id = 107, name = "abc7", classId = 3, Salary = 260 });  
         obj.Add(new student { id = 108, name = "abc8", classId = 3, Salary = 290 });  
         return obj;  
       }  
     }  
     static void Main(string[] args)  
     {  
       student s = new student();  
       var students = s.getStudents().ToList();  
       var z = students.GroupBy(x => x.classId).Select(g => new {ClassId=g.Key, TotalSalary= g.Sum(x=>x.Salary) });  
       foreach (var item in z)  
       {  
         Console.WriteLine(item.ClassId);  
         Console.WriteLine(item.TotalSalary);  
         Console.WriteLine();  
       }  
       Console.ReadLine();  
     }  
   }  
 }  


O/P:
















using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 namespace LINQ  
 {  
   class Program  
   {  
     class Parent  
     {  
       public int Id { get; set; }  
       public string Val { get; set; }  
     }  
     class Child  
     {  
       public int Id { get; set; }  
       public string ChildVal { get; set; }  
     }  
     static void Main(string[] args)  
     {  
       List<Parent> plist = new List<Parent>() {  
         new Parent{ Id=1, Val="X"},  
         new Parent{ Id=2, Val="Y"},  
         new Parent{ Id=3, Val="Z"}  
       };  
       List<Child> clist = new List<Child>() {  
         new Child{ Id=1, ChildVal="X1"},  
         new Child{ Id=1, ChildVal="X2"},  
         new Child{ Id=1, ChildVal="X3"},  
         new Child{ Id=2, ChildVal="Y1"},  
         new Child{ Id=2, ChildVal="Y2"},  
         new Child{ Id=4, ChildVal="Y3"},  
       };  
       var joinedResult = from p in plist  
                 join c in clist on p.Id equals c.Id  
             select new { p.Val, c.ChildVal };  
       Console.WriteLine("===JOIN===");  
       foreach (var item in joinedResult)  
       {  
         Console.WriteLine(item.Val);  
         Console.WriteLine(item.ChildVal);  
         Console.WriteLine();  
       }  
       Console.WriteLine("===GROUPJOIN===");  
       var y = from p in plist  
           join c in clist on p.Id equals c.Id into g  
           select new { Parent = p, Children = g };  
       foreach (var item in y)  
       {  
         Console.WriteLine(item.Parent.Id);  
         foreach (var item1 in item.Children)  
         {  
           Console.WriteLine(item1.ChildVal);  
         }  
         Console.WriteLine();  
       }  
       Console.ReadLine();  
     }  
   }  
 }  



Second Highest Salary using LINQ

Topic

Finding the Second Highest or nth highest using Linq

Scenario

  • List of Students. Each having name,Salary etc
  • Find the second highest or nth highest salary
Code

using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 namespace XML  
 {  
   class Program  
   {  
     public class student  
     {  
       public int id { get; set; }  
       public string name { get; set; }  
       public int classId { get; set; }  
       public int Salary { get; set; }  
       public List<student> getStudents()  
       {  
         List<student> obj = new List<student>();  
         obj.Add(new student { id = 101, name = "abc1", classId = 1, Salary = 100 });  
         obj.Add(new student { id = 102, name = "abc2", classId = 1, Salary = 200 });  
         obj.Add(new student { id = 103, name = "abc3", classId = 2, Salary = 300 });  
         obj.Add(new student { id = 104, name = "abc4", classId = 2, Salary = 800 });  
         obj.Add(new student { id = 105, name = "abc5", classId = 2, Salary = 900 });  
         obj.Add(new student { id = 106, name = "abc6", classId = 3, Salary = 250 });  
         obj.Add(new student { id = 107, name = "abc7", classId = 3, Salary = 260 });  
         obj.Add(new student { id = 108, name = "abc8", classId = 3, Salary = 290 });  
         return obj;  
       }  
     }  
     static void Main(string[] args)  
     {  
       student s = new student();  
       var students = s.getStudents().ToList();  
       var firsthighest = students.OrderByDescending(x => x.Salary).Select(x => x.Salary).First();  
       var secondhighest = students.OrderByDescending(x => x.Salary).Where(x => x.Salary != firsthighest).Select(x => x.Salary).First();  
       var secondhighest1 = students.OrderByDescending(x => x.Salary).Skip(2).Select(x => x.Salary).First();  
       var secondhighest2 = students.Where(x => x.Salary != (students.Max(x1 => x1.Salary))).Select(x => x.Salary).Max();  
       Console.WriteLine(secondhighest1);  
       Console.WriteLine(secondhighest2);  
       Console.ReadLine();  
     }  
   }  
 }  


Output




Thursday, 31 July 2014

To Find uncommon things between two List. And how interesting it is to know Concat Union and Distinct works

In the Last blog we saw how to find the uncommon things in particular list.
http://linqdetails.blogspot.in/2014/07/llinq-except.html


Now we need to find the uncommon things in both the list. We will continue with same example. First we will start with simple integer type , then we will go for class.


using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Collections;  
 class Program  
 {  
   static void Main(string[] args)  
   {  
     List<int> num1 = new List<int> { 1, 4, 7, 12 };  
     List<int> num2 = new List<int> { 1, 9, 7, 13, 13 };  
     List<int> num2NotinNum1 = num2.Where(x => !num1.Contains(x)).Distinct().ToList();// 4 12  
     List<int> num1NotinNum2 = num1.Where(x => !num2.Contains(x)).Distinct().ToList(); //9 13  
     List<int> CommoninNum1Num2 = num1.Intersect(num2).ToList(); // 1 7  
     List<int> uncommonBoth = num1.Union(num2).Except(CommoninNum1Num2).ToList();  
     foreach (int uncommon in uncommonBoth)  
     {  
       Console.WriteLine(uncommon);  
     }  
     Console.ReadLine();  
   }  
 }  










It works for

List<int> uncommonBoth = num1.Concat(num2).Except(CommoninNum1Num2).ToList();

So what is the difference between Concat and Union ???

I have explained here.

http://linqdetails.blogspot.com/2014/07/difference-between-concat-and-union.html

================================================================

using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Collections;  
 class list1  
 {  
   public string FirstName { get; set; }  
   public string Id { get; set; }  
 }  
 class list2  
 {  
   public string FirstName { get; set; }  
   public string Id { get; set; }  
 }  
 class Program  
 {  
   static void Main(string[] args)  
   {  
     List<list1> list1 = new List<list1>();  
     List<list2> list2 = new List<list2>();  
     list1.Add(new list1 { FirstName = "Biswa", Id = "1" });  
     list1.Add(new list1 { FirstName = "Anurag", Id = "2" });  
     list1.Add(new list1 { FirstName = "Prasad", Id = "3" });  
     list2.Add(new list2 { FirstName = "Abhi", Id = "1" });  
     list2.Add(new list2 { FirstName = "Anurag", Id = "2" });  
     list2.Add(new list2 { FirstName = "Anurag", Id = "3" });  
     list2.Add(new list2 { FirstName = "Siba", Id = "3" });  
     List<list1> list1NotInList2 = list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName)).ToList();  
     List<list2> list2NotInList1 = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName)).ToList();  
     Console.WriteLine("====Items in LIst1 but Not in list2===========");  
     foreach (list1 items in list1NotInList2)  
     {  
       Console.WriteLine(items.FirstName + " " + items.Id);  
     }  
     Console.WriteLine("====Items in LIst2 but Not in list1============");  
     foreach (list2 items in list2NotInList1)  
     {  
       Console.WriteLine(items.FirstName + " " + items.Id);  
     }  
   }  
 }  





           






-------------------Now we want both the things---------------------------------------------------------------


var finalcommonresult = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName)).ToList().Cast<object>().Union  
               (list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName)).ToList().Cast<object>());  
       foreach (dynamic items in finalcommonresult)  
       {  
         Console.WriteLine(items.FirstName + " " + items.Id);  
       }  

Biswa 1
Prasad 3
Abhi 1
Siba 3

-- This gives me both the resullts.
-- We can use Concat too
-- You need to use dynamic otherwise you will get error. items will be resolved at runtime whether its from list1 or list2. if from list1 then at runtime the firstname will be resolved as of list1.

----------------------------------------------------------------------------------------------------------

list1.Add(new list1 { FirstName = "Biswa", Id = "1" });  
       list1.Add(new list1 { FirstName = "Anurag", Id = "7" });  
       list1.Add(new list1 { FirstName = "Biswa", Id = "3" });  
       list2.Add(new list2 { FirstName = "Abhi", Id = "1" });  
       list2.Add(new list2 { FirstName = "Anurag", Id = "2" });  
       list2.Add(new list2 { FirstName = "Anurag", Id = "3" });  
       list2.Add(new list2 { FirstName = "Siba", Id = "3" });  
 var finalcommonresult = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
             .Select(x=>x.FirstName).ToList().Cast<object>()  
             .Union(list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName))  
             .Select(x => x.FirstName).ToList().Cast<object>());
       


Here we get
Abhi 
Siba
Biswa

The same thing with concat will give

 var finalcommonresult = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
             .Select(x=>x.FirstName).ToList().Cast<object>()  
             .Concat(list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName)).ToList()  
             .Select(x=>x.FirstName).Cast<object>()); 

Abhi
Siba
Biswa
Biswa

--------------------------------------------------------------------------------------------------------------------------


 list1.Add(new list1 { FirstName = "Biswa", Id = "1" });  
 list1.Add(new list1 { FirstName = "Anurag", Id = "7" });  
 list1.Add(new list1 { FirstName = "Biswa", Id = "1" });  
 list2.Add(new list2 { FirstName = "Abhi", Id = "1" });  
 list2.Add(new list2 { FirstName = "Anurag", Id = "2" });  
 list2.Add(new list2 { FirstName = "Anurag", Id = "3" });  
 list2.Add(new list2 { FirstName = "Siba", Id = "3" });  
 var finalcommonresult = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
 .ToList().Cast<object>().Union(list1.Where(x => !list2  
 .Any(y => y.FirstName == x.FirstName)).ToList().Cast<object>());

            
Here we get

Abhi   1
Siba    3
Biswa 1
Biswa 1

If we are not selecting anything even if both same the records [Biswa 1 and Biswa 1] are same, union gives all the results.
-----------------------------------------------------------------------------------------------
So lets try selecting both the items. Earlier we have selected First Name. Now to select both items use anonymous type. Use the new keyword followed by curly braces.

 var finalcommonresult = list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
 .Select( x=> new { x.FirstName,x.Id}).ToList().Cast<object>()  
 .Union(list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName))  
 .Select(x => new { x.FirstName, x.Id }).ToList().Cast<object>()); 

Abhi   1
Siba    3
Biswa 1

Cool :)

-------------------------------------------------------------------------------------------------------


 list1.Add(new list1 { FirstName = "Biswa", Id = "1" });  
 list1.Add(new list1 { FirstName = "Anurag", Id = "7" });  
 list1.Add(new list1 { FirstName = "Biswa", Id = "9" });  
 list2.Add(new list2 { FirstName = "Abhi", Id = "1" });  
 list2.Add(new list2 { FirstName = "Anurag", Id = "2" });  
 list2.Add(new list2 { FirstName = "Anurag", Id = "3" });  
 list2.Add(new list2 { FirstName = "Siba", Id = "3" });  
 var finalcommonresult = list2  
 .Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
 .Select( x=> new { x.FirstName,x.Id}).ToList().Cast<object>()  
 .Union(list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName)).Select(x => new { x.FirstName, x.Id }).ToList().Cast<object>());           


Abhi   1
Siba    3
Biswa 1
Biswa 9

ok cool :)

--------------------------------------------------------------------------------------------------------
You can also use Distinct for above queries.

Distinct also works when we are filtering on anonymous types.

var distinctlist1 = list1.Select(x=> new {x.FirstName,x.Id}).ToList().Distinct(); // this will give distinct  
  var distinctlist11 = list1.ToList().Distinct();//this will not give distinct 

So we can use Distinct + Concat. Because Distinct + Union will not make any sense as Union will take the distinct if we are working on anonymous types. 



var finalcommonresult =   
 list2.Where(x => !list1.Any(y => y.FirstName == x.FirstName))  
 .Select(x=> new {x.FirstName,x.Id}).ToList().Distinct().Cast<object>()  
 .Concat(list1.Where(x => !list2.Any(y => y.FirstName == x.FirstName))  
 .Select(x => new { x.FirstName, x.Id }).ToList().Distinct().Cast<object>());



To select a distinct you need to do as anonymous type too.

---------------------------------------------------------------------------------------------------------










Difference Between Concat and Union


Code
class Program  
   {  
     static void Main(string[] args)  
     {  
       List<int> num1 = new List<int> { 1, 4, 7, 12 };  
       List<int> num2 = new List<int> { 1, 9, 7, 13, 13,13 };  
       List<int> concat = num1.Concat(num2).ToList();  
       List<int> union = num1.Union(num2).ToList();  
       Console.WriteLine("=======Concat Begins=======");  
       foreach (int concatnum in concat)  
       {  
         Console.WriteLine(concatnum);  
       }  
       Console.WriteLine("=======Union Begins=======");  
       foreach (int unionnum in union)  
       {  
         Console.WriteLine(unionnum);  
       }  
    }  
 }  

Output
















Union gives the distinct values. But Concat gives us all the things between both the list.
By this fact we can say that Concat() is faster than Union() because it doesn't do any processing. 

Wednesday, 30 July 2014

Convert One List to Another List without For loop or Foreach Loop



  • Convert One List to Another List

Code
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Collections;  
 namespace OverloadingConstructor  
 {  
   class TargetList  
   {  
     public TargetList(string Id,string FirstName,String LastName)  
     {  
       this.LastName = LastName;  
       this.FirstName = FirstName;  
       this.Id = Id;  
     }  
     public string Id { get; set; }  
     public string FirstName { get; set; }  
     public string LastName { get; set; }  
   }  
   class SourceList  
   {  
     public string Id { get; set; }  
     public string FirstName { get; set; }  
     public string LastName { get; set; }  
   }  
   class Program  
   {  
     public static TargetList PointFToPoint(SourceList pf)  
     {  
       return new TargetList(pf.Id,pf.FirstName,pf.LastName);  
     }  
     static void Main(string[] args)  
     {  
       List<SourceList> sourceList = new List<SourceList>();  
       sourceList.Add(new SourceList { FirstName = "Anurag", LastName = "Nayak", Id = "1" });  
       sourceList.Add(new SourceList { FirstName = "Abhishek", LastName = "Nayak", Id = "2" });  
       sourceList.Add(new SourceList { FirstName = "Siba", LastName = "Dalai", Id = "3" });  
       sourceList.Add(new SourceList { FirstName = "Manju", LastName = "Rath", Id = "4" });  
       List<TargetList> Target = sourceList.ConvertAll(new Converter<SourceList, TargetList>(PointFToPoint));  
       foreach (var item in Target)  
       {  
         Console.WriteLine("First Name::- " + item.FirstName + "; Last Name::- " +item.LastName + "; Id:- " + item.Id);  
       }  
       Console.ReadLine();  
     }  
   }  
 }  

Debug



Output









Second Way

Code
using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Collections;  
 namespace OverloadingConstructor  
 {  
   class TargetList  
   {  
     public string Id { get; set; }  
     public string FirstName { get; set; }  
     public string LastName { get; set; }  
     public List<string> DepId { get; set; }  
   }  
   class SourceList  
   {  
     public string Id { get; set; }  
     public string FirstName { get; set; }  
     public string LastName { get; set; }  
     public List<string> DepId { get; set; }  
   }  
   class Program  
   {  
     static void Main(string[] args)  
     {  
       List<SourceList> sourceList = new List<SourceList>();  
       sourceList.Add(new SourceList { FirstName = "Anurag", LastName = "Nayak", Id = "1", DepId = new List<string> { "1","2","3"} });  
       sourceList.Add(new SourceList { FirstName = "Abhishek", LastName = "Nayak", Id = "2", DepId = new List<string> { "11", "21", "31" } });  
       sourceList.Add(new SourceList { FirstName = "Siba", LastName = "Dalai", Id = "3", DepId = new List<string> { "21", "22", "23" } });  
       sourceList.Add(new SourceList { FirstName = "Manju", LastName = "Rath", Id = "4", DepId = new List<string> { "31", "32", "33" } });  
       List<TargetList> Target1 = sourceList.ConvertAll(x => new TargetList { FirstName=x.FirstName,LastName=x.LastName, Id=x.Id, DepId=x.DepId}).ToList();  
       foreach (var item in Target1)  
       {  
         Console.WriteLine("First Name::- " + item.FirstName + "; Last Name::- " + item.LastName + "; Id:- " + item.Id);  
       }   
       Console.ReadLine();  
     }  
   }  
 }  

Debug