c# - Ternary operator in LINQ query is not working as expected -


i'm trying filter list of objects based on conditions. i've seen few posts use ternary operators in linq query accomplish this.

at bottom of code snippet linq query. expected test each where, instead seems try first where , leave rest. list not contain data expect to. doing wrong?

    private observablecollection<logevent> m_logevents = applicationdata.mainlogentries.logevents;     public observablecollection<logevent> logevents     {         { return m_logevents; }         set { m_logevents = value; raisepropertychanged("logevents"); }     }      private bool m_viewdebuglogs = false;     public bool viewdebuglogs     {         { return m_viewdebuglogs; }         set { m_viewdebuglogs = value; raisepropertychanged("viewdebuglogs"); filterlist(); }     }      private bool m_viewerrorlogs = true;     public bool viewerrorlogs     {         { return m_viewerrorlogs; }         set { m_viewerrorlogs = value; raisepropertychanged("viewerrorlogs"); filterlist(); }     }      private bool m_viewinfologs = true;     public bool viewinfologs     {         { return m_viewinfologs; }         set { m_viewinfologs = value; raisepropertychanged("viewinfologs"); filterlist(); }     }      private void filterlist()     {         list<logevent> selectedevents = (from x in logevents                                          (viewdebuglogs) ? x.level == "debug" : false                                          (viewerrorlogs) ? x.level == "error" : false                                          (viewinfologs) ? x.level == "info" : false                                          select x).tolist();      } 

edit: goal of if appropriate bool viewdebuglogs, viewerrorlogs, or viewinfologs, or combination of 3 enabled logs filtered appropriate types.

i've tried following answers no success:

this gets debug logs:

            list<logevent> selectedevents = (from x in logevents                                          (viewdebuglogs == true) ? x.level == "debug" : false                                          || (viewerrorlogs == true) ? x.level == "error" : false                                          || (viewinfologs == true) ? x.level == "info" : false                                          select x).tolist(); 

this gets debug logs:

            list<logevent> selectedevents = (from x in logevents                                          (viewdebuglogs == true) ? x.level == "debug" : true                                          || (viewerrorlogs == true) ? x.level == "error" : true                                          || (viewinfologs == true) ? x.level == "info" : true                                          select x).tolist(); 

this gets nothing:

            list<logevent> selectedevents = (from x in logevents                                          (viewdebuglogs == true) ? x.level == "debug" : true                                          (viewerrorlogs == true) ? x.level == "error" : true                                          (viewinfologs == true) ? x.level == "info" : true                                          select x).tolist(); 

you use

 list<logevent> selectedevents = (from x in logevents                            (viewdebuglogs && x.level == "debug")                                || (viewerrorlogs && x.level == "error")                               || (viewinfologs  && x.level == "info")                             select x).tolist(); 

that clearer (in opinion). note ( ) around expression parts: want make sure of order in ands , ors evaluated.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -