0
SiteCode    |     date      |softwarename  |  Success  |      Error
ASZ             2017-08-5         a              1             1
ASZ             2017-08-5         b              2             2
ASZ             2017-08-5         a              3             3
ASZ             2017-08-6         a              6             6
NBL             2017-08-5         a              5             5


var CurrentCustomer = Database.Session.Query<Customer>()
    .FirstOrDefault(x => x.deleted_at == null 
        && x.Customer_Username == User.Identity.Name);

if(CurrentCustomer != null) 
{
    var CurrentDepValues = Database.Session.Query<deployments>()
        .Where(x => x.SiteCode == CurrentCustomer.SiteCode 
            && x.softwarename == current_type)
        .OrderBy(x => x.date)
        .ToList();
}

I have a db like that. I want to get an output like below. The same date values should be sum.

SiteCode    |     date      |softwarename  |  Success  |      Error
ASZ             2017-08-5         a              4             4
ASZ             2017-08-6         a              6             6
2
  • 1
    You can use GroupBy(x=>x.date).Select(blah).ToList() Commented Sep 5, 2017 at 18:14
  • But, how will I sum successes and errors? Commented Sep 5, 2017 at 18:16

1 Answer 1

1

Using a GroupBy:

if(CurrentCustomer != null) {
     var CurrentDepValues = Database.Session.Query<deployments>()
      .Where(x => x.SiteCode == CurrentCustomer.SiteCode 
          && x.softwarename == current_type)
      .GroupBy (x => new {x.SiteCode, x.date, x.softwarename})
      .Select (g => new {
            SiteCode = g.Key.SiteCode,
            Date = g.Key.date,
            SoftwareName = g.Key.softwarename,
            Success = g.Sum(x => x.Success),
            Error = g.Sum(x => x.Error)
      })
      .OrderBy(x => x.Date)
      .ToList();
}   

EDIT

The above will create an anonymous type. If you would like to get List<Models.deployments>, you should do so in the select lambda:

.Select (g => new Models.deployments {
            SiteCode = g.Key.SiteCode,
            Date = g.Key.date,
            SoftwareName = g.Key.softwarename,
            Success = g.Sum(x => x.Success),
            Error = g.Sum(x => x.Error)
      })
Sign up to request clarification or add additional context in comments.

1 Comment

I have one more question. I can not transfer it to view with the current ViewModel. (public IList<Models.deployments> deployments { get; set; }) How do I write in the ViewModel?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.