GroupBy with Conditional SUMS

by ssi 1. May 2013 15:03
    private static void Main(string[] args)
        {
            DataClasses1DataContext dc = new DataClasses1DataContext(DEFAULT_CONNECTION_STRING);
            int nMonth = 2;
            int nYear = 2013;
            for (int index = 0; index < 5; index++)
             
            {
                nMonth = index;
                Console.WriteLine(string.Format("Month: {0}",nMonth));
                var qry = dc.tbl_ssi_LoanSnapshot_Documents.Where(c => c.TakenMonth == nMonth
                             & c.TakenYear == nYear)
                            .GroupBy(g => g.DocType)
                              .Select(g => new
               {
                   DocType = g.Key
                   ,
                   DocCount = g.Count(p => p.Request_Date != null)
                   ,
                   DocsReqRecd = g.Count(p => p.Request_Date != null & p.Recieved_Date != null)
                   ,
                   DocsReqNOTRecd = g.Count(p => p.Request_Date != null & p.Recieved_Date == null)
 
               })
               .OrderBy(o => o.DocType);
                foreach (var doc in qry)
                {
                    if (doc.DocCount > 100)
                        Console.WriteLine(string.Format("Month: {0} Doc: {1} Count:{2} ReqRecd: {3} ReqNOTRecd: {4}"
                                 , nMonth , doc.DocType, doc.DocCount, doc.DocsReqRecd, doc.DocsReqNOTRecd));
                }
                Console.WriteLine("");
            }
            Console.ReadLine();
        }

Tags: ,

CSharp

Calendar

<<  May 2024  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

RecentComments

None

Development Team @ Shelbysys

We develop custom database applications for our clients. Our development tool of choice is MS Visual Studio. 

Quotations

"Courage is fear holding on a minute longer"
General George Patton Jr