Linq

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

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


No comments:

Post a Comment