locked
Query for Lamda Expression RRS feed

  • Question

  • User179837873 posted

    Hi,

    I need help to write lamda expression for below query

    SELECT SUM(InvoicesSentCount) 'invoicesCount' ,
    COUNT(*) Emails ,
    DATEPART(YEAR, SentTime) year ,
    DATEPART(MONTH, SentTime) month ,
    DATEPART(DAY, SentTime) day,
    MIN(SentTime) AS 'FirstSent', MAX(SentTime) AS 'LastSent'
    FROM dbo.InvoiceSentMailerLog
    GROUP BY DATEPART(YEAR, SentTime) ,
    DATEPART(MONTH, SentTime) ,
    DATEPART(DAY, SentTime);

    Please advise me how to write above query in the lamda expression

    Monday, January 18, 2016 4:27 AM

Answers

  • User-271186128 posted

    Hi RamBhopal Reddy,

    Please advise me how to write above query in the lamda expression

    You could try to use the following code:

                    var query = dbo.InvoiceSentMailerLog.GroupBy(c => new { c.SentTime.Value.Year, c.SentTime.Value.Month, c.SentTime.Value.Day })
                        .Select(c => new { 
                            Emails = c.Count(), 
                            Year = c.Key.Year, 
                            Month = c.Key.Month, 
                            Day = c.Key.Day, 
                            FirstSent = c.Min(d=>d.SentTime), 
                            LastSent = c.Max(d=>d.SentTime), 
                            InvoiceCount = c.Sum(d=>d.InvoicesSentCount)}).ToList();

    I suggest you could refer to this article: https://msdn.microsoft.com/en-us/library/bb399367(v=vs.110).aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 19, 2016 2:36 AM

All replies

  • User1577371250 posted

    Hi,

    Check this query

     var query = (from p in db.InvoiceSentMailerLog
                group p by new { Year = p.SentTime.Year, Month = p.SentTime.Month, Day = p.SentTime.Day } into r
                             select new
                             {
                                 InvoicesCount = r.Sum(c => c.InvoicesSentCount),
                                 EmailCount = r.Count(),
                                 Year = r.Key.Year,
                                 Month = r.Key.Month,
                                 Day = r.Key.Day,
                                 FirstSent = r.Min(c => c.SentTime),
                                 LastSent = r.Max(c => c.SentTime)
    
                             }).ToList();

    Monday, January 18, 2016 11:15 AM
  • User-271186128 posted

    Hi RamBhopal Reddy,

    Please advise me how to write above query in the lamda expression

    You could try to use the following code:

                    var query = dbo.InvoiceSentMailerLog.GroupBy(c => new { c.SentTime.Value.Year, c.SentTime.Value.Month, c.SentTime.Value.Day })
                        .Select(c => new { 
                            Emails = c.Count(), 
                            Year = c.Key.Year, 
                            Month = c.Key.Month, 
                            Day = c.Key.Day, 
                            FirstSent = c.Min(d=>d.SentTime), 
                            LastSent = c.Max(d=>d.SentTime), 
                            InvoiceCount = c.Sum(d=>d.InvoicesSentCount)}).ToList();

    I suggest you could refer to this article: https://msdn.microsoft.com/en-us/library/bb399367(v=vs.110).aspx

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 19, 2016 2:36 AM