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

Add comment

biuquote
  • Comment
  • Preview
Loading

Calendar

<<  May 2026  >>
MoTuWeThFrSaSu
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

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

"Fill the unforgiving minute with sixty seconds worth of distance run."
Rudyard Kipling