C#

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

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));
            Console.ReadLine();
        }
    }

}

----Another Nice way to convert is 

List<TargetList> Target1 = sourceList.ConvertAll(x => new TargetList {  FirstName=x.FirstName, Id=x.Id}).ToList();


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, Id=x.Id, DepId=x.DepId}).ToList();
            Console.ReadLine();
        }
    }

}







No comments:

Post a Comment