Karthi N
Karthi N

Reputation: 3

Getting the wrong count?

I am getting the wrong count error and I can't figure out why.

In my database serverId = 1/2/3 has only one started value and 3 notstarted value for each server.

painfo = (from paes in server.AppPM_Paes
          where (paes.PaStatus == "Started" || paes.PaStatus == "NotStarted" ) && paes.ServerId != null
          select new PaDetails { ServerID = paes.ServerId, PaStatus = paes.PaStatus }).ToList();
foreach (PaDetails a in painfo)
{
    if (a.PaStatus.Contains("Started") && a.ServerID.Equals(1))
        stCount1++;
    if (a.PaStatus.Contains("Started") && a.ServerID.Equals(2))
        stCount2++;
    if (a.PaStatus.Contains("Started") && a.ServerID.Equals(3))
        stCount3++;
    if (a.PaStatus.Contains("NotStarted") && a.ServerID.Equals(1))
        notStCount1++;
    if (a.PaStatus.Contains("NotStarted") && a.ServerID.Equals(2))
        notStCount2++;
    if (a.PaStatus.Contains("NotStarted") && a.ServerID.Equals(3))
        notStCount3++;                    
}       

But in my above code (stCount#->started count), stCount# has the value 4 instead of 1.

What's wrong in my code?

Can you please help me?

Upvotes: 0

Views: 70

Answers (1)

Adam Plocher
Adam Plocher

Reputation: 14243

You're doing a .Contains("Started"). This will include any string that has the word "Started" in it, including "NotStarted"

You can change it to a.PaStatus == "Started" or a.PaStatus.Equals("Started")

Upvotes: 1

Related Questions