adc90
adc90

Reputation: 303

Getting the incorrect count from a LINQ query

I'm running a LINQ query some records that I want to update and take the count of before putting them back into the database. I'm running into trouble when I try to take a count on a Linq query that has been reassigned in the second conditional.

graduates = graduates.Where(m => m.NotifySentInd == false);

However the query just like it near the top returns the correct amount of users in the query when I select the option "AllStudents" but never in the second conditional or with the similar query I have at the top when choosing "ApprovedNotYetSent"

 public void ApproveAdultEdStudents(string Option, string CurrentYear)
 {
        var graduates = _unitOfWork.GraduateRepository.Graduates.Where(m => m.StudentTypeCd == "A" && m.SchoolYear == CurrentYear);
        System.Diagnostics.Debug.WriteLine("A: " + graduates.Count());  

        var gradsSent = graduates.Where(m => m.NotifySentInd == false);
        System.Diagnostics.Debug.WriteLine("B: " + gradsSent.Count());

        if (Option == "AllStudents") {
            foreach (var item in graduates) {
                item.NotifySentInd = true;
                item.DiplomaOrderDt = DateTime.Now;
                item.DiplomaOrderToVendorInd = false;
            }
            System.Diagnostics.Debug.WriteLine("C: " + graduates.Count());
        } else if (Option == "ApproveNotYetSent") {
            graduates = graduates.Where(m => m.NotifySentInd == false);
            foreach (var item in graduates) {
                item.NotifySentInd = true;
                item.DiplomaOrderDt = DateTime.Now;
                item.DiplomaOrderToVendorInd = false;
            }
            System.Diagnostics.Debug.WriteLine("D: " + graduates.Count());
        }
  }

When I code I get the following results.

AllStudents
A: 69
B: 68
C: 69

ApproveNotYetSent
A: 0
B: 0
D: 0

Upvotes: 0

Views: 626

Answers (3)

yuuzo
yuuzo

Reputation: 337

I could've answered you by a comment but I don't have enough rep to do it, I think there's a typo in your code:

   else if (Option == "ApproveNotYetSent")

Should be "Aproved" no ? Anyway this is very bad practive, you should always work with Strongly Typed variables, create an enum for your options and call it OptionType then you can do something like this:

public void ApproveAdultEdStudents(OptionType Option, string CurrentYear)
{
    ...
    if(Option == OptionType.AllStudents)
        //do stuff
    ...
]

Upvotes: 2

Cameron
Cameron

Reputation: 2594

To expand upon Daniel A. White's comment:

don't re-enumerate the results.

You need do evaluate your result set, which basically means append ToList() or ToArray() to the end of it.

var graduates = _unitOfWork.GraduateRepository.Graduates
                           .Where(m => m.StudentTypeCd == "A" && 
                                       m.SchoolYear == CurrentYear)
                           .ToList(); // This is the line you're missing

Without this, you'll be re-querying the database for results every time and creating new objects, thus messing up your counts.

Upvotes: 3

Radin Gospodinov
Radin Gospodinov

Reputation: 2323

Try that: graduates = graduates.Where(m => m.NotifySentInd == false).ToList(); This will create a list and then all the Counts will be the same.

Upvotes: 1

Related Questions