Tech vidhyarthi
Tech vidhyarthi

Reputation: 79

OR operator is not working in LINQ

I need to select a list which is not contains grade 2 and grade 8 . Now all the items with grade 2 also selects. Please see below is my code

var subjectList = printViewModel.GetSubjects().Where(p => p.Grade != "2" || p.Grade != "8");

PrintTemplateViewModel class

public class PrintTemplateViewModel
{
    public List<SubjectsViewModel> lstSubjectsViewModel { get; set; }

    public List<SubjectsViewModel> GetSubjects()
    {
        return lstSubjectsViewModel;
    }
}

public class SubjectsViewModel  
{ 
    public string Grade { get; set; } 
}

Upvotes: 0

Views: 416

Answers (2)

mnsr
mnsr

Reputation: 12437

The "OR" is working fine. It is your logic that is not working :D

use && because you want to check both values are true.

Upvotes: 0

Peter Duniho
Peter Duniho

Reputation: 70652

You want &&, not ||. No matter what the string value, it's always going to either not be "2" or not be "8". It can't be both at once! :)

Upvotes: 5

Related Questions