Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my project,

I have Tow Table
(1).Import_detail table (Date, Description,Parti_Name, Company_name,Amount ) and
(2).Export_detail table (Date, Description,Parti_Name, Company_name,Amount )


and i want to Show this two table detail as Balance sheet in single DataGrid using linq. here i am using union to merge column but i don,t know how to get remain Balance as
Date , Description, Parti_Name, Company_name, Debit,Credit, Balance 





Solution:

XML
I have 3 classes
public class Import
    {
        public DateTime date { get; set; }
        public decimal amount { get; set; }
    }

public class Export
    {
        public DateTime date { get; set; }
        public decimal amount { get; set; }
    }

public class Result
    {
        public DateTime date { get; set; }
        public decimal creditAmount { get; set; }
        public decimal debitAmount { get; set; }
        public decimal balanceAmount { get; set; }
    }


And Select

XML
var importList = new List<Import>()
                {
                    new Import{date = DateTime.Parse("2009-01-02"), amount = 10000},
                    new Import{date = DateTime.Parse("2009-01-25"), amount = 6000}
                };

                var exportList = new List<Export>()
                {
                    new Export{date = DateTime.Parse("2009-01-05"), amount = 500},
                    new Export{date = DateTime.Parse("2009-01-10"), amount = 1000},
                    new Export{date = DateTime.Parse("2009-01-11"), amount = 1500},
                    new Export{date = DateTime.Parse("2009-01-15"), amount = 4000},
                    new Export{date = DateTime.Parse("2009-01-28"), amount = 5000}
                };


And i Get from this Query

C#
var temp =
importList.Select(c =&gt; new Result { date = c.date, debitAmount = c.amount, creditAmount = 0, balanceAmount = 0 })
.Union(exportList.Select(c =&gt; new Result { date = c.date, debitAmount = 0, creditAmount = c.amount, balanceAmount = 0 }))
.GroupBy(c =&gt; c.date)
.Select(c =&gt; new Result { date = c.Key, creditAmount = c.Sum(g =&gt; g.creditAmount), debitAmount = c.Sum(g =&gt; g.debitAmount) })
.OrderBy(c =&gt; c.date).ToList();

 foreach (var item in temp) item.balanceAmount = temp
.Where(c =&gt; c.date &lt; item.date).OrderByDescending(c =&gt; c.date)
.Select(c =&gt; c.balanceAmount).FirstOrDefault() + item.debitAmount - item.creditAmount;


Result::

debitAmount  creditAmount balanceAmount
10000        0            10000
0            500          9500
0            1000         8500
0            1500         7000
0            4000         3000
6000         0            9000 
0            5000         4000 
Posted
Updated 24-Jan-15 8:24am
v3
Comments
Wendelius 23-Jan-15 1:19am    
Can you give an example source data
George Swan 23-Jan-15 1:51am    
Could you please provide some more information?
Which column is the key that links the two databases?
Which column do you want to group the data by?
Which entries are credit and which are debit transactions?
Is the balance a running balance ordered by date?
mukesh mourya 24-Jan-15 14:29pm    
I solve my self ...thakyou :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900