2

I have a treatmentDate column in the database where dates to treated patients are submitted. If I need to generate report of Total Sum say between January 01 2015 and Jan 30 2015 for a particular company, how do I construct my query.

Below is what I have done and I'm having errors

            var treatmentSum = (from s in db.Treatments
                                where (s.CompanyId == CompanyID)
                                s.TreatmentDate >= fromDate && s.TreatmentDate <= toDate
                                select s.Amount).Sum();                
            ViewBag.TreatmentSum = treatmentSum;

Here's the treatment date column I want to search through. enter image description here

Your guidance will be highly appreciated.

3
  • " I'm having errors"? What errors? Commented May 15, 2015 at 13:25
  • 1
    shouldn't there be an '&&' between (s.CompanyId == CompanyID) AND s.TreatmentDate Commented May 15, 2015 at 13:28
  • Any luck with the answers below: stackoverflow.com/a/30263549/1837329 Commented May 16, 2015 at 18:57

2 Answers 2

2

You didn't post the error message. You are trying to compare strings using the <= and >= , instead try to handle all of your Date and Time variable as DateTime properties. Here's a piece of code for the LINQ expression:

DateTime FromDate= Convert.ToDateTime("04/20/2015");
DateTime ToDate= Convert.ToDateTime("04/28/2015");
ViewBag.TreatmentSum = GetAllTreatments.
Where(c=>Convert.ToDateTime(c.TreatmentDate)>=FromDate &&
Convert.ToDateTime(c.TreatmentDate)<=ToDate).
Sum(c=>c.Amount); 

Also Here's a link to a demo for your specific problem : https://dotnetfiddle.net/1a50HI

Sign up to request clarification or add additional context in comments.

3 Comments

It worked like a charm. Thanks a bunch for your help. The resukts was generated and sent to a PDF view. So I can see the results directly in PDF now. Any ideas on how to take the entire PDF view and save in into the database? I used the rotavita PDF generator.
Post the code on a separate question for that and me or someone else will definitely help.
Hi thank once again. I have posted the question as suggested. You can find it here. link
1

You've got some formatting issues:

  var treatmentSum = (from s in db.Treatments
                            where s.CompanyId == CompanyID &&
                            s.TreatmentDate >= fromDate && s.TreatmentDate <= toDate
                            select s.Amount).Sum();                
        ViewBag.TreatmentSum = treatmentSum;

You had a where in the middle of the statement:

where (s.CompanyId == CompanyID) and you weren't separating it with &&

Edit

As stated the dates are in string, you could convert them inline, although be aware you may need to provide some CultureInfo so the formatter knows how to convert the date:

  var treatmentSum = (from s in db.Treatments
                                where s.CompanyId == CompanyID &&
                                Convert.ToDateTime(s.TreatmentDate) >= fromDate && Convert.ToDateTime(s.TreatmentDate) <= toDate
                                select s.Amount).Sum();       

1 Comment

I added the && to the query, I ran into another red squiggly line saying Operator '>=' cannot be appied to operands of type 'string and 'string' I actually have my date column in the database as a string datatype then used the datetime data annotation In my BOL.

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.