1

I'm trying to query a DataGridView based on a search term and further refine the result based on CheckBox status. I have the following code

var memberIdSearch =    from m in context.Members
                        where m.MemberId == idSearch
                        where checkBoxActive.Checked && m.MemberStatus == "Active"
                        where checkBoxInactive.Checked && m.MemberStatus == "Inactive"
                        select m;

When querying, no matter the search term I enter, no results are returned regardless of CheckBox status. If i comment out the checkbox lines, the query returns all entries matching the search term

What I'm trying to achieve

If memberid matches search term, if active checkbox is ticked, display all display all active members, and if inactive checkbox is ticked, also display inactive members

I'm sure this is something simple, but I can't work it out

Rick D
  • 139
  • 1
  • 11
  • is it?? the checkbox names are different in the 2 where clauses, or am i missing something? – Rick D Nov 26 '17 at 21:54
  • Ah well, overlooked that, but the essence is it contains `m.MemberStatus == "Active" && m.MemberStatus == "Inactive"`. – Gert Arnold Nov 26 '17 at 21:56
  • so the way I'm dissecting the query is... if memeberID matches, and active is ticked, show all active members, and if inactive is ticked, also show inactive members. How would I write that if my query is not n correct – Rick D Nov 26 '17 at 22:02
  • You better compose your query by `Or` predicates like [this](https://stackoverflow.com/a/14622200/861716). – Gert Arnold Nov 26 '17 at 22:12

1 Answers1

1

Try to group all statements in one:

var memberIdSearch =    from m in context.Members
                        where m.MemberId == idSearch && 
                        (checkBoxActive.Checked && m.MemberStatus == "Active" ||
                         checkBoxInactive.Checked && m.MemberStatus == "Inactive")
                        select m;
melya
  • 578
  • 5
  • 24
  • @melya I would suggest you separate the two conditions with brackets. `((checkBoxActive.Checked && m.MemberStatus == "Active") || (checkBoxInactive.Checked && m.MemberStatus == "Inactive"))` this has a potential of yielding unexpected results – Stephen Nov 27 '17 at 06:55
  • @Stephen according to operators precedence there should be no problem. but with brackets the readability is a bit better. – melya Nov 27 '17 at 09:41