Thursday, January 3, 2013

Sample joining two object data


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace CustomClass
{
    class Program
    {
        static void Main(string[] args)
        {

            CustomCustomerInfo[] cc = new []{
                new CustomCustomerInfo {accountnumber="100",name="ABC COMPANY",address1="ABC ADDRESS",address2="",city="ABC CITY",
                region="ABC REGION"},
                new CustomCustomerInfo {accountnumber="200",name="PQR COMPANY",address1="PQR ADDRESS",address2="",city="PQR CITY",
                region="PQR REGION"},
                new CustomCustomerInfo {accountnumber="300",name="XYZ COMPANY",address1="XYZ ADDRESS",address2="",city="XYZ CITY",
                region="XYZ REGION"}
            };

            CustomAgingInfo[] aa = new[]{
                new CustomAgingInfo {accountnumber="100",debtcurrent="11",debt30="15"},
                new CustomAgingInfo {accountnumber="100",debtcurrent="13",debt30="14"},
                new CustomAgingInfo {accountnumber="100",debtcurrent="12",debt30="10"},
                new CustomAgingInfo {accountnumber="200",debtcurrent="14",debt30="1"},
                new CustomAgingInfo {accountnumber="300",debtcurrent="15",debt30="9"},
                new CustomAgingInfo {accountnumber="300",debtcurrent="19",debt30="8"},
                new CustomAgingInfo {accountnumber="300",debtcurrent="18",debt30="16"},
            };


            var query = from cu in cc
                        join ag in aa on cu.accountnumber equals ag.accountnumber into agg
                        select new { cu, agg };


            foreach (var data in query)
            {
                foreach(var agi in data.agg){
                    Console.WriteLine(data.cu.accountnumber + "|" + data.cu.name + "|" + data.cu.address1
                        + "|" + data.cu.address2+ "|" + data.cu.city + "|" + data.cu.region
                        + "|" + agi.debtcurrent + "|" + agi.debt30);
                }
            }


        }
    }

    class CustomCustomerInfo
    {

        public string accountnumber
        {
            get;
            set;
        }

        public string name
        {
            get;
            set;
        }
        public string address1
        {
            get;
            set;
        }
        public string address2
        {
            get;
            set;
        }
        public string city
        {
            get;
            set;
        }
        public string region
        {
            get;
            set;
        }

        public override string ToString()
        {
            return string.Format("{0}|{1}|{2}|{3}|{4}|{5}", accountnumber, name, address1, address2, city, region);
        }
    }


    class CustomAgingInfo
    {

        public string accountnumber
        {
            get;
            set;
        }

        public string debtcurrent
        {
            get;
            set;
        }
        public string debt30
        {
            get;
            set;
        }
     

       
    }
}

No comments:

Post a Comment

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code