[{"data":1,"prerenderedAt":1523},["ShallowReactive",2],{"blog:2021:or-expressions-in-linq":3,"blogMore-Development":1509,"comments-or-expressions-in-linq":1522},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":16,"body":31,"_type":1500,"_id":1501,"_source":1502,"_file":1503,"_stem":1504,"_extension":1505,"url":1506,"wordCount":1507,"minutes":138,"commentCount":1508},"/blog/2021/or-expressions-in-linq","2021",false,"en","Creating OR expressions in LINQ","As everybody who has read my blog before knows, I love LINQ and miss it when coding in other languages, so it's nice when I get a chance to use it again. When I come back to it with fresh eyes, I notice some things aren't as easy as they should be - and this time is no exception.","2021-03-02T10:03:00-08:00","Development",[13,14,15],".NET","LINQ","C#",{"type":17,"children":18},"root",[19,26],{"type":20,"tag":21,"props":22,"children":23},"element","p",{},[24],{"type":25,"value":9},"text",{"type":20,"tag":21,"props":27,"children":28},{},[29],{"type":25,"value":30},"People often need to build up LINQ expressions at runtime based on filters or criteria a user has selected. Adding criteria is incredibly easy, as you can chain operations together on the IQueryable interface, e.g.",{"type":17,"children":32,"toc":1493},[33,37,44,48,216,221,227,232,485,490,496,501,1242,1247,1426,1432,1445,1450,1456,1469,1482,1487],{"type":20,"tag":21,"props":34,"children":35},{},[36],{"type":25,"value":9},{"type":20,"tag":38,"props":39,"children":41},"h2",{"id":40},"background",[42],{"type":25,"value":43},"Background",{"type":20,"tag":21,"props":45,"children":46},{},[47],{"type":25,"value":30},{"type":20,"tag":49,"props":50,"children":55},"pre",{"className":51,"code":52,"language":53,"meta":54,"style":54},"language-csharp shiki shiki-themes everforest-light dracula","if (customerActive)\n  query = query.Where(c => c.IsActive);\nif (customerCountry != null)\n  query = query.Where(c => c.Country == customerCountry);\n","csharp","",[56],{"type":20,"tag":57,"props":58,"children":59},"code",{"__ignoreMap":54},[60,78,136,165],{"type":20,"tag":61,"props":62,"children":65},"span",{"class":63,"line":64},"line",1,[66,72],{"type":20,"tag":61,"props":67,"children":69},{"style":68},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[70],{"type":25,"value":71},"if",{"type":20,"tag":61,"props":73,"children":75},{"style":74},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[76],{"type":25,"value":77}," (customerActive)\n",{"type":20,"tag":61,"props":79,"children":81},{"class":63,"line":80},2,[82,87,93,98,104,109,115,120,125,131],{"type":20,"tag":61,"props":83,"children":84},{"style":74},[85],{"type":25,"value":86},"  query ",{"type":20,"tag":61,"props":88,"children":90},{"style":89},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[91],{"type":25,"value":92},"=",{"type":20,"tag":61,"props":94,"children":95},{"style":74},[96],{"type":25,"value":97}," query.",{"type":20,"tag":61,"props":99,"children":101},{"style":100},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[102],{"type":25,"value":103},"Where",{"type":20,"tag":61,"props":105,"children":106},{"style":74},[107],{"type":25,"value":108},"(",{"type":20,"tag":61,"props":110,"children":112},{"style":111},"--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[113],{"type":25,"value":114},"c",{"type":20,"tag":61,"props":116,"children":117},{"style":89},[118],{"type":25,"value":119}," =>",{"type":20,"tag":61,"props":121,"children":122},{"style":74},[123],{"type":25,"value":124}," c.",{"type":20,"tag":61,"props":126,"children":128},{"style":127},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[129],{"type":25,"value":130},"IsActive",{"type":20,"tag":61,"props":132,"children":133},{"style":74},[134],{"type":25,"value":135},");\n",{"type":20,"tag":61,"props":137,"children":139},{"class":63,"line":138},3,[140,144,149,154,160],{"type":20,"tag":61,"props":141,"children":142},{"style":68},[143],{"type":25,"value":71},{"type":20,"tag":61,"props":145,"children":146},{"style":74},[147],{"type":25,"value":148}," (customerCountry ",{"type":20,"tag":61,"props":150,"children":151},{"style":89},[152],{"type":25,"value":153},"!=",{"type":20,"tag":61,"props":155,"children":157},{"style":156},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[158],{"type":25,"value":159}," null",{"type":20,"tag":61,"props":161,"children":162},{"style":74},[163],{"type":25,"value":164},")\n",{"type":20,"tag":61,"props":166,"children":168},{"class":63,"line":167},4,[169,173,177,181,185,189,193,197,201,206,211],{"type":20,"tag":61,"props":170,"children":171},{"style":74},[172],{"type":25,"value":86},{"type":20,"tag":61,"props":174,"children":175},{"style":89},[176],{"type":25,"value":92},{"type":20,"tag":61,"props":178,"children":179},{"style":74},[180],{"type":25,"value":97},{"type":20,"tag":61,"props":182,"children":183},{"style":100},[184],{"type":25,"value":103},{"type":20,"tag":61,"props":186,"children":187},{"style":74},[188],{"type":25,"value":108},{"type":20,"tag":61,"props":190,"children":191},{"style":111},[192],{"type":25,"value":114},{"type":20,"tag":61,"props":194,"children":195},{"style":89},[196],{"type":25,"value":119},{"type":20,"tag":61,"props":198,"children":199},{"style":74},[200],{"type":25,"value":124},{"type":20,"tag":61,"props":202,"children":203},{"style":127},[204],{"type":25,"value":205},"Country",{"type":20,"tag":61,"props":207,"children":208},{"style":89},[209],{"type":25,"value":210}," ==",{"type":20,"tag":61,"props":212,"children":213},{"style":74},[214],{"type":25,"value":215}," customerCountry);\n",{"type":20,"tag":21,"props":217,"children":218},{},[219],{"type":25,"value":220},"This is great if you want to AND things together, which is often the case in building up filters. Other operations, like Contains, are useful in allowing many options against a specific field.",{"type":20,"tag":38,"props":222,"children":224},{"id":223},"problem",[225],{"type":25,"value":226},"Problem",{"type":20,"tag":21,"props":228,"children":229},{},[230],{"type":25,"value":231},"But what about offering a choice involving either of two properties? One way to write it would be:",{"type":20,"tag":49,"props":233,"children":235},{"className":51,"code":234,"language":53,"meta":54,"style":54},"if (customerActive) {\n  if (customerEnterprise) {\n    query = query.Where(c => c.IsActive || c.IsEnterprise);\n  } else {\n    query = query.Where(c => c.IsActive);\n  } else {\n    if (customerEnterprise) {\n      query = query.Where(c => c.IsEnterprise);\n    }\n  }\n}\n",[236],{"type":20,"tag":57,"props":237,"children":238},{"__ignoreMap":54},[239,251,264,322,340,384,400,413,458,467,476],{"type":20,"tag":61,"props":240,"children":241},{"class":63,"line":64},[242,246],{"type":20,"tag":61,"props":243,"children":244},{"style":68},[245],{"type":25,"value":71},{"type":20,"tag":61,"props":247,"children":248},{"style":74},[249],{"type":25,"value":250}," (customerActive) {\n",{"type":20,"tag":61,"props":252,"children":253},{"class":63,"line":80},[254,259],{"type":20,"tag":61,"props":255,"children":256},{"style":68},[257],{"type":25,"value":258},"  if",{"type":20,"tag":61,"props":260,"children":261},{"style":74},[262],{"type":25,"value":263}," (customerEnterprise) {\n",{"type":20,"tag":61,"props":265,"children":266},{"class":63,"line":138},[267,272,276,280,284,288,292,296,300,304,309,313,318],{"type":20,"tag":61,"props":268,"children":269},{"style":74},[270],{"type":25,"value":271},"    query ",{"type":20,"tag":61,"props":273,"children":274},{"style":89},[275],{"type":25,"value":92},{"type":20,"tag":61,"props":277,"children":278},{"style":74},[279],{"type":25,"value":97},{"type":20,"tag":61,"props":281,"children":282},{"style":100},[283],{"type":25,"value":103},{"type":20,"tag":61,"props":285,"children":286},{"style":74},[287],{"type":25,"value":108},{"type":20,"tag":61,"props":289,"children":290},{"style":111},[291],{"type":25,"value":114},{"type":20,"tag":61,"props":293,"children":294},{"style":89},[295],{"type":25,"value":119},{"type":20,"tag":61,"props":297,"children":298},{"style":74},[299],{"type":25,"value":124},{"type":20,"tag":61,"props":301,"children":302},{"style":127},[303],{"type":25,"value":130},{"type":20,"tag":61,"props":305,"children":306},{"style":89},[307],{"type":25,"value":308}," ||",{"type":20,"tag":61,"props":310,"children":311},{"style":74},[312],{"type":25,"value":124},{"type":20,"tag":61,"props":314,"children":315},{"style":127},[316],{"type":25,"value":317},"IsEnterprise",{"type":20,"tag":61,"props":319,"children":320},{"style":74},[321],{"type":25,"value":135},{"type":20,"tag":61,"props":323,"children":324},{"class":63,"line":167},[325,330,335],{"type":20,"tag":61,"props":326,"children":327},{"style":74},[328],{"type":25,"value":329},"  } ",{"type":20,"tag":61,"props":331,"children":332},{"style":68},[333],{"type":25,"value":334},"else",{"type":20,"tag":61,"props":336,"children":337},{"style":74},[338],{"type":25,"value":339}," {\n",{"type":20,"tag":61,"props":341,"children":343},{"class":63,"line":342},5,[344,348,352,356,360,364,368,372,376,380],{"type":20,"tag":61,"props":345,"children":346},{"style":74},[347],{"type":25,"value":271},{"type":20,"tag":61,"props":349,"children":350},{"style":89},[351],{"type":25,"value":92},{"type":20,"tag":61,"props":353,"children":354},{"style":74},[355],{"type":25,"value":97},{"type":20,"tag":61,"props":357,"children":358},{"style":100},[359],{"type":25,"value":103},{"type":20,"tag":61,"props":361,"children":362},{"style":74},[363],{"type":25,"value":108},{"type":20,"tag":61,"props":365,"children":366},{"style":111},[367],{"type":25,"value":114},{"type":20,"tag":61,"props":369,"children":370},{"style":89},[371],{"type":25,"value":119},{"type":20,"tag":61,"props":373,"children":374},{"style":74},[375],{"type":25,"value":124},{"type":20,"tag":61,"props":377,"children":378},{"style":127},[379],{"type":25,"value":130},{"type":20,"tag":61,"props":381,"children":382},{"style":74},[383],{"type":25,"value":135},{"type":20,"tag":61,"props":385,"children":387},{"class":63,"line":386},6,[388,392,396],{"type":20,"tag":61,"props":389,"children":390},{"style":74},[391],{"type":25,"value":329},{"type":20,"tag":61,"props":393,"children":394},{"style":68},[395],{"type":25,"value":334},{"type":20,"tag":61,"props":397,"children":398},{"style":74},[399],{"type":25,"value":339},{"type":20,"tag":61,"props":401,"children":403},{"class":63,"line":402},7,[404,409],{"type":20,"tag":61,"props":405,"children":406},{"style":68},[407],{"type":25,"value":408},"    if",{"type":20,"tag":61,"props":410,"children":411},{"style":74},[412],{"type":25,"value":263},{"type":20,"tag":61,"props":414,"children":416},{"class":63,"line":415},8,[417,422,426,430,434,438,442,446,450,454],{"type":20,"tag":61,"props":418,"children":419},{"style":74},[420],{"type":25,"value":421},"      query ",{"type":20,"tag":61,"props":423,"children":424},{"style":89},[425],{"type":25,"value":92},{"type":20,"tag":61,"props":427,"children":428},{"style":74},[429],{"type":25,"value":97},{"type":20,"tag":61,"props":431,"children":432},{"style":100},[433],{"type":25,"value":103},{"type":20,"tag":61,"props":435,"children":436},{"style":74},[437],{"type":25,"value":108},{"type":20,"tag":61,"props":439,"children":440},{"style":111},[441],{"type":25,"value":114},{"type":20,"tag":61,"props":443,"children":444},{"style":89},[445],{"type":25,"value":119},{"type":20,"tag":61,"props":447,"children":448},{"style":74},[449],{"type":25,"value":124},{"type":20,"tag":61,"props":451,"children":452},{"style":127},[453],{"type":25,"value":317},{"type":20,"tag":61,"props":455,"children":456},{"style":74},[457],{"type":25,"value":135},{"type":20,"tag":61,"props":459,"children":461},{"class":63,"line":460},9,[462],{"type":20,"tag":61,"props":463,"children":464},{"style":74},[465],{"type":25,"value":466},"    }\n",{"type":20,"tag":61,"props":468,"children":470},{"class":63,"line":469},10,[471],{"type":20,"tag":61,"props":472,"children":473},{"style":74},[474],{"type":25,"value":475},"  }\n",{"type":20,"tag":61,"props":477,"children":479},{"class":63,"line":478},11,[480],{"type":20,"tag":61,"props":481,"children":482},{"style":74},[483],{"type":25,"value":484},"}\n",{"type":20,"tag":21,"props":486,"children":487},{},[488],{"type":25,"value":489},"This code is already hard to read and is going to increase in complexity for each new option.",{"type":20,"tag":38,"props":491,"children":493},{"id":492},"solution",[494],{"type":25,"value":495},"Solution",{"type":20,"tag":21,"props":497,"children":498},{},[499],{"type":25,"value":500},"With some helper code, we can combine two expression predicates into a single OrElse for use in a Where condition.",{"type":20,"tag":49,"props":502,"children":504},{"className":51,"code":503,"language":53,"meta":54,"style":54},"class Or {\n  public static Expression\u003CFunc\u003CT, bool>> Combine\u003CT>(Expression\u003CFunc\u003CT, bool>> left, Expression\u003CFunc\u003CT, bool>> right) {\n    if (left == null && right == null) throw new ArgumentException(\"At least one argument must not be null\");\n    if (left == null) return right;\n    if (right == null) return left;\n\n    var parameter = Expression.Parameter(typeof(T), \"p\");\n    var combined = new ParameterReplacer(parameter).Visit(Expression.OrElse(left.Body, right.Body));\n    return Expression.Lambda\u003CFunc\u003CT, bool>>(combined, parameter);\n  }\n\n  class ParameterReplacer : ExpressionVisitor {\n    readonly ParameterExpression parameter;\n\n    internal ParameterReplacer(ParameterExpression parameter) {\n      this.parameter = parameter;\n    }\n\n    protected override Expression VisitParameter(ParameterExpression node) {\n      return parameter;\n    }\n  }\n}\n",[505],{"type":20,"tag":57,"props":506,"children":507},{"__ignoreMap":54},[508,526,683,766,799,832,841,906,975,1021,1028,1035,1062,1081,1089,1120,1149,1157,1165,1205,1218,1226,1234],{"type":20,"tag":61,"props":509,"children":510},{"class":63,"line":64},[511,516,522],{"type":20,"tag":61,"props":512,"children":513},{"style":68},[514],{"type":25,"value":515},"class",{"type":20,"tag":61,"props":517,"children":519},{"style":518},"--shiki-default:#3A94C5;--shiki-dark:#8BE9FD",[520],{"type":25,"value":521}," Or",{"type":20,"tag":61,"props":523,"children":524},{"style":74},[525],{"type":25,"value":339},{"type":20,"tag":61,"props":527,"children":528},{"class":63,"line":80},[529,534,539,545,550,555,559,564,569,575,580,585,589,594,599,604,608,612,616,620,624,628,632,637,641,645,649,653,657,661,665,669,673,678],{"type":20,"tag":61,"props":530,"children":531},{"style":89},[532],{"type":25,"value":533},"  public",{"type":20,"tag":61,"props":535,"children":536},{"style":89},[537],{"type":25,"value":538}," static",{"type":20,"tag":61,"props":540,"children":542},{"style":541},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[543],{"type":25,"value":544}," Expression",{"type":20,"tag":61,"props":546,"children":547},{"style":74},[548],{"type":25,"value":549},"\u003C",{"type":20,"tag":61,"props":551,"children":552},{"style":541},[553],{"type":25,"value":554},"Func",{"type":20,"tag":61,"props":556,"children":557},{"style":74},[558],{"type":25,"value":549},{"type":20,"tag":61,"props":560,"children":561},{"style":541},[562],{"type":25,"value":563},"T",{"type":20,"tag":61,"props":565,"children":566},{"style":74},[567],{"type":25,"value":568},", ",{"type":20,"tag":61,"props":570,"children":572},{"style":571},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[573],{"type":25,"value":574},"bool",{"type":20,"tag":61,"props":576,"children":577},{"style":74},[578],{"type":25,"value":579},">> ",{"type":20,"tag":61,"props":581,"children":582},{"style":100},[583],{"type":25,"value":584},"Combine",{"type":20,"tag":61,"props":586,"children":587},{"style":74},[588],{"type":25,"value":549},{"type":20,"tag":61,"props":590,"children":592},{"style":591},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[593],{"type":25,"value":563},{"type":20,"tag":61,"props":595,"children":596},{"style":74},[597],{"type":25,"value":598},">(",{"type":20,"tag":61,"props":600,"children":601},{"style":541},[602],{"type":25,"value":603},"Expression",{"type":20,"tag":61,"props":605,"children":606},{"style":74},[607],{"type":25,"value":549},{"type":20,"tag":61,"props":609,"children":610},{"style":541},[611],{"type":25,"value":554},{"type":20,"tag":61,"props":613,"children":614},{"style":74},[615],{"type":25,"value":549},{"type":20,"tag":61,"props":617,"children":618},{"style":541},[619],{"type":25,"value":563},{"type":20,"tag":61,"props":621,"children":622},{"style":74},[623],{"type":25,"value":568},{"type":20,"tag":61,"props":625,"children":626},{"style":571},[627],{"type":25,"value":574},{"type":20,"tag":61,"props":629,"children":630},{"style":74},[631],{"type":25,"value":579},{"type":20,"tag":61,"props":633,"children":634},{"style":111},[635],{"type":25,"value":636},"left",{"type":20,"tag":61,"props":638,"children":639},{"style":74},[640],{"type":25,"value":568},{"type":20,"tag":61,"props":642,"children":643},{"style":541},[644],{"type":25,"value":603},{"type":20,"tag":61,"props":646,"children":647},{"style":74},[648],{"type":25,"value":549},{"type":20,"tag":61,"props":650,"children":651},{"style":541},[652],{"type":25,"value":554},{"type":20,"tag":61,"props":654,"children":655},{"style":74},[656],{"type":25,"value":549},{"type":20,"tag":61,"props":658,"children":659},{"style":541},[660],{"type":25,"value":563},{"type":20,"tag":61,"props":662,"children":663},{"style":74},[664],{"type":25,"value":568},{"type":20,"tag":61,"props":666,"children":667},{"style":571},[668],{"type":25,"value":574},{"type":20,"tag":61,"props":670,"children":671},{"style":74},[672],{"type":25,"value":579},{"type":20,"tag":61,"props":674,"children":675},{"style":111},[676],{"type":25,"value":677},"right",{"type":20,"tag":61,"props":679,"children":680},{"style":74},[681],{"type":25,"value":682},") {\n",{"type":20,"tag":61,"props":684,"children":685},{"class":63,"line":138},[686,690,695,700,704,709,714,718,722,727,732,737,742,746,752,758,762],{"type":20,"tag":61,"props":687,"children":688},{"style":68},[689],{"type":25,"value":408},{"type":20,"tag":61,"props":691,"children":692},{"style":74},[693],{"type":25,"value":694}," (left ",{"type":20,"tag":61,"props":696,"children":697},{"style":89},[698],{"type":25,"value":699},"==",{"type":20,"tag":61,"props":701,"children":702},{"style":156},[703],{"type":25,"value":159},{"type":20,"tag":61,"props":705,"children":706},{"style":89},[707],{"type":25,"value":708}," &&",{"type":20,"tag":61,"props":710,"children":711},{"style":74},[712],{"type":25,"value":713}," right ",{"type":20,"tag":61,"props":715,"children":716},{"style":89},[717],{"type":25,"value":699},{"type":20,"tag":61,"props":719,"children":720},{"style":156},[721],{"type":25,"value":159},{"type":20,"tag":61,"props":723,"children":724},{"style":74},[725],{"type":25,"value":726},") ",{"type":20,"tag":61,"props":728,"children":729},{"style":68},[730],{"type":25,"value":731},"throw",{"type":20,"tag":61,"props":733,"children":734},{"style":68},[735],{"type":25,"value":736}," new",{"type":20,"tag":61,"props":738,"children":739},{"style":541},[740],{"type":25,"value":741}," ArgumentException",{"type":20,"tag":61,"props":743,"children":744},{"style":74},[745],{"type":25,"value":108},{"type":20,"tag":61,"props":747,"children":749},{"style":748},"--shiki-default:#8DA101;--shiki-dark:#E9F284",[750],{"type":25,"value":751},"\"",{"type":20,"tag":61,"props":753,"children":755},{"style":754},"--shiki-default:#8DA101;--shiki-dark:#F1FA8C",[756],{"type":25,"value":757},"At least one argument must not be null",{"type":20,"tag":61,"props":759,"children":760},{"style":748},[761],{"type":25,"value":751},{"type":20,"tag":61,"props":763,"children":764},{"style":74},[765],{"type":25,"value":135},{"type":20,"tag":61,"props":767,"children":768},{"class":63,"line":167},[769,773,777,781,785,789,794],{"type":20,"tag":61,"props":770,"children":771},{"style":68},[772],{"type":25,"value":408},{"type":20,"tag":61,"props":774,"children":775},{"style":74},[776],{"type":25,"value":694},{"type":20,"tag":61,"props":778,"children":779},{"style":89},[780],{"type":25,"value":699},{"type":20,"tag":61,"props":782,"children":783},{"style":156},[784],{"type":25,"value":159},{"type":20,"tag":61,"props":786,"children":787},{"style":74},[788],{"type":25,"value":726},{"type":20,"tag":61,"props":790,"children":791},{"style":68},[792],{"type":25,"value":793},"return",{"type":20,"tag":61,"props":795,"children":796},{"style":74},[797],{"type":25,"value":798}," right;\n",{"type":20,"tag":61,"props":800,"children":801},{"class":63,"line":342},[802,806,811,815,819,823,827],{"type":20,"tag":61,"props":803,"children":804},{"style":68},[805],{"type":25,"value":408},{"type":20,"tag":61,"props":807,"children":808},{"style":74},[809],{"type":25,"value":810}," (right ",{"type":20,"tag":61,"props":812,"children":813},{"style":89},[814],{"type":25,"value":699},{"type":20,"tag":61,"props":816,"children":817},{"style":156},[818],{"type":25,"value":159},{"type":20,"tag":61,"props":820,"children":821},{"style":74},[822],{"type":25,"value":726},{"type":20,"tag":61,"props":824,"children":825},{"style":68},[826],{"type":25,"value":793},{"type":20,"tag":61,"props":828,"children":829},{"style":74},[830],{"type":25,"value":831}," left;\n",{"type":20,"tag":61,"props":833,"children":834},{"class":63,"line":386},[835],{"type":20,"tag":61,"props":836,"children":838},{"emptyLinePlaceholder":837},true,[839],{"type":25,"value":840},"\n",{"type":20,"tag":61,"props":842,"children":843},{"class":63,"line":402},[844,849,854,858,863,868,872,877,881,885,890,894,898,902],{"type":20,"tag":61,"props":845,"children":846},{"style":571},[847],{"type":25,"value":848},"    var",{"type":20,"tag":61,"props":850,"children":851},{"style":74},[852],{"type":25,"value":853}," parameter ",{"type":20,"tag":61,"props":855,"children":856},{"style":89},[857],{"type":25,"value":92},{"type":20,"tag":61,"props":859,"children":860},{"style":74},[861],{"type":25,"value":862}," Expression.",{"type":20,"tag":61,"props":864,"children":865},{"style":100},[866],{"type":25,"value":867},"Parameter",{"type":20,"tag":61,"props":869,"children":870},{"style":74},[871],{"type":25,"value":108},{"type":20,"tag":61,"props":873,"children":874},{"style":68},[875],{"type":25,"value":876},"typeof",{"type":20,"tag":61,"props":878,"children":879},{"style":74},[880],{"type":25,"value":108},{"type":20,"tag":61,"props":882,"children":883},{"style":541},[884],{"type":25,"value":563},{"type":20,"tag":61,"props":886,"children":887},{"style":74},[888],{"type":25,"value":889},"), ",{"type":20,"tag":61,"props":891,"children":892},{"style":748},[893],{"type":25,"value":751},{"type":20,"tag":61,"props":895,"children":896},{"style":754},[897],{"type":25,"value":21},{"type":20,"tag":61,"props":899,"children":900},{"style":748},[901],{"type":25,"value":751},{"type":20,"tag":61,"props":903,"children":904},{"style":74},[905],{"type":25,"value":135},{"type":20,"tag":61,"props":907,"children":908},{"class":63,"line":415},[909,913,918,922,926,931,936,941,946,951,956,961,966,970],{"type":20,"tag":61,"props":910,"children":911},{"style":571},[912],{"type":25,"value":848},{"type":20,"tag":61,"props":914,"children":915},{"style":74},[916],{"type":25,"value":917}," combined ",{"type":20,"tag":61,"props":919,"children":920},{"style":89},[921],{"type":25,"value":92},{"type":20,"tag":61,"props":923,"children":924},{"style":68},[925],{"type":25,"value":736},{"type":20,"tag":61,"props":927,"children":928},{"style":541},[929],{"type":25,"value":930}," ParameterReplacer",{"type":20,"tag":61,"props":932,"children":933},{"style":74},[934],{"type":25,"value":935},"(parameter).",{"type":20,"tag":61,"props":937,"children":938},{"style":100},[939],{"type":25,"value":940},"Visit",{"type":20,"tag":61,"props":942,"children":943},{"style":74},[944],{"type":25,"value":945},"(Expression.",{"type":20,"tag":61,"props":947,"children":948},{"style":100},[949],{"type":25,"value":950},"OrElse",{"type":20,"tag":61,"props":952,"children":953},{"style":74},[954],{"type":25,"value":955},"(left.",{"type":20,"tag":61,"props":957,"children":958},{"style":127},[959],{"type":25,"value":960},"Body",{"type":20,"tag":61,"props":962,"children":963},{"style":74},[964],{"type":25,"value":965},", right.",{"type":20,"tag":61,"props":967,"children":968},{"style":127},[969],{"type":25,"value":960},{"type":20,"tag":61,"props":971,"children":972},{"style":74},[973],{"type":25,"value":974},"));\n",{"type":20,"tag":61,"props":976,"children":977},{"class":63,"line":460},[978,983,987,992,996,1000,1004,1008,1012,1016],{"type":20,"tag":61,"props":979,"children":980},{"style":68},[981],{"type":25,"value":982},"    return",{"type":20,"tag":61,"props":984,"children":985},{"style":74},[986],{"type":25,"value":862},{"type":20,"tag":61,"props":988,"children":989},{"style":100},[990],{"type":25,"value":991},"Lambda",{"type":20,"tag":61,"props":993,"children":994},{"style":74},[995],{"type":25,"value":549},{"type":20,"tag":61,"props":997,"children":998},{"style":541},[999],{"type":25,"value":554},{"type":20,"tag":61,"props":1001,"children":1002},{"style":74},[1003],{"type":25,"value":549},{"type":20,"tag":61,"props":1005,"children":1006},{"style":541},[1007],{"type":25,"value":563},{"type":20,"tag":61,"props":1009,"children":1010},{"style":74},[1011],{"type":25,"value":568},{"type":20,"tag":61,"props":1013,"children":1014},{"style":571},[1015],{"type":25,"value":574},{"type":20,"tag":61,"props":1017,"children":1018},{"style":74},[1019],{"type":25,"value":1020},">>(combined, parameter);\n",{"type":20,"tag":61,"props":1022,"children":1023},{"class":63,"line":469},[1024],{"type":20,"tag":61,"props":1025,"children":1026},{"style":74},[1027],{"type":25,"value":475},{"type":20,"tag":61,"props":1029,"children":1030},{"class":63,"line":478},[1031],{"type":20,"tag":61,"props":1032,"children":1033},{"emptyLinePlaceholder":837},[1034],{"type":25,"value":840},{"type":20,"tag":61,"props":1036,"children":1038},{"class":63,"line":1037},12,[1039,1044,1048,1053,1058],{"type":20,"tag":61,"props":1040,"children":1041},{"style":68},[1042],{"type":25,"value":1043},"  class",{"type":20,"tag":61,"props":1045,"children":1046},{"style":518},[1047],{"type":25,"value":930},{"type":20,"tag":61,"props":1049,"children":1050},{"style":74},[1051],{"type":25,"value":1052}," : ",{"type":20,"tag":61,"props":1054,"children":1055},{"style":541},[1056],{"type":25,"value":1057},"ExpressionVisitor",{"type":20,"tag":61,"props":1059,"children":1060},{"style":74},[1061],{"type":25,"value":339},{"type":20,"tag":61,"props":1063,"children":1065},{"class":63,"line":1064},13,[1066,1071,1076],{"type":20,"tag":61,"props":1067,"children":1068},{"style":89},[1069],{"type":25,"value":1070},"    readonly",{"type":20,"tag":61,"props":1072,"children":1073},{"style":541},[1074],{"type":25,"value":1075}," ParameterExpression",{"type":20,"tag":61,"props":1077,"children":1078},{"style":74},[1079],{"type":25,"value":1080}," parameter;\n",{"type":20,"tag":61,"props":1082,"children":1084},{"class":63,"line":1083},14,[1085],{"type":20,"tag":61,"props":1086,"children":1087},{"emptyLinePlaceholder":837},[1088],{"type":25,"value":840},{"type":20,"tag":61,"props":1090,"children":1092},{"class":63,"line":1091},15,[1093,1098,1102,1106,1111,1116],{"type":20,"tag":61,"props":1094,"children":1095},{"style":89},[1096],{"type":25,"value":1097},"    internal",{"type":20,"tag":61,"props":1099,"children":1100},{"style":100},[1101],{"type":25,"value":930},{"type":20,"tag":61,"props":1103,"children":1104},{"style":74},[1105],{"type":25,"value":108},{"type":20,"tag":61,"props":1107,"children":1108},{"style":541},[1109],{"type":25,"value":1110},"ParameterExpression",{"type":20,"tag":61,"props":1112,"children":1113},{"style":111},[1114],{"type":25,"value":1115}," parameter",{"type":20,"tag":61,"props":1117,"children":1118},{"style":74},[1119],{"type":25,"value":682},{"type":20,"tag":61,"props":1121,"children":1123},{"class":63,"line":1122},16,[1124,1130,1135,1140,1145],{"type":20,"tag":61,"props":1125,"children":1127},{"style":1126},"--shiki-default:#DF69BA;--shiki-default-font-style:inherit;--shiki-dark:#BD93F9;--shiki-dark-font-style:italic",[1128],{"type":25,"value":1129},"      this",{"type":20,"tag":61,"props":1131,"children":1132},{"style":74},[1133],{"type":25,"value":1134},".",{"type":20,"tag":61,"props":1136,"children":1137},{"style":127},[1138],{"type":25,"value":1139},"parameter",{"type":20,"tag":61,"props":1141,"children":1142},{"style":89},[1143],{"type":25,"value":1144}," =",{"type":20,"tag":61,"props":1146,"children":1147},{"style":74},[1148],{"type":25,"value":1080},{"type":20,"tag":61,"props":1150,"children":1152},{"class":63,"line":1151},17,[1153],{"type":20,"tag":61,"props":1154,"children":1155},{"style":74},[1156],{"type":25,"value":466},{"type":20,"tag":61,"props":1158,"children":1160},{"class":63,"line":1159},18,[1161],{"type":20,"tag":61,"props":1162,"children":1163},{"emptyLinePlaceholder":837},[1164],{"type":25,"value":840},{"type":20,"tag":61,"props":1166,"children":1168},{"class":63,"line":1167},19,[1169,1174,1179,1183,1188,1192,1196,1201],{"type":20,"tag":61,"props":1170,"children":1171},{"style":89},[1172],{"type":25,"value":1173},"    protected",{"type":20,"tag":61,"props":1175,"children":1176},{"style":89},[1177],{"type":25,"value":1178}," override",{"type":20,"tag":61,"props":1180,"children":1181},{"style":541},[1182],{"type":25,"value":544},{"type":20,"tag":61,"props":1184,"children":1185},{"style":100},[1186],{"type":25,"value":1187}," VisitParameter",{"type":20,"tag":61,"props":1189,"children":1190},{"style":74},[1191],{"type":25,"value":108},{"type":20,"tag":61,"props":1193,"children":1194},{"style":541},[1195],{"type":25,"value":1110},{"type":20,"tag":61,"props":1197,"children":1198},{"style":111},[1199],{"type":25,"value":1200}," node",{"type":20,"tag":61,"props":1202,"children":1203},{"style":74},[1204],{"type":25,"value":682},{"type":20,"tag":61,"props":1206,"children":1208},{"class":63,"line":1207},20,[1209,1214],{"type":20,"tag":61,"props":1210,"children":1211},{"style":68},[1212],{"type":25,"value":1213},"      return",{"type":20,"tag":61,"props":1215,"children":1216},{"style":74},[1217],{"type":25,"value":1080},{"type":20,"tag":61,"props":1219,"children":1221},{"class":63,"line":1220},21,[1222],{"type":20,"tag":61,"props":1223,"children":1224},{"style":74},[1225],{"type":25,"value":466},{"type":20,"tag":61,"props":1227,"children":1229},{"class":63,"line":1228},22,[1230],{"type":20,"tag":61,"props":1231,"children":1232},{"style":74},[1233],{"type":25,"value":475},{"type":20,"tag":61,"props":1235,"children":1237},{"class":63,"line":1236},23,[1238],{"type":20,"tag":61,"props":1239,"children":1240},{"style":74},[1241],{"type":25,"value":484},{"type":20,"tag":21,"props":1243,"children":1244},{},[1245],{"type":25,"value":1246},"Now we can:",{"type":20,"tag":49,"props":1248,"children":1250},{"className":51,"code":1249,"language":53,"meta":54,"style":54},"Expression\u003CFunc\u003CCustomer, bool>> criteria = null;\nif (customerActive)\n    criteria = Or.Combine(criteria, c => c.IsActive == customerActive);\nif (customerEnterprise)\n    criteria = Or.Combine(criteria, c => c.IsEnterprise == customerEnterprise);\n",[1251],{"type":20,"tag":57,"props":1252,"children":1253},{"__ignoreMap":54},[1254,1304,1315,1366,1378],{"type":20,"tag":61,"props":1255,"children":1256},{"class":63,"line":64},[1257,1261,1265,1269,1273,1278,1282,1286,1291,1295,1299],{"type":20,"tag":61,"props":1258,"children":1259},{"style":541},[1260],{"type":25,"value":603},{"type":20,"tag":61,"props":1262,"children":1263},{"style":74},[1264],{"type":25,"value":549},{"type":20,"tag":61,"props":1266,"children":1267},{"style":541},[1268],{"type":25,"value":554},{"type":20,"tag":61,"props":1270,"children":1271},{"style":74},[1272],{"type":25,"value":549},{"type":20,"tag":61,"props":1274,"children":1275},{"style":541},[1276],{"type":25,"value":1277},"Customer",{"type":20,"tag":61,"props":1279,"children":1280},{"style":74},[1281],{"type":25,"value":568},{"type":20,"tag":61,"props":1283,"children":1284},{"style":571},[1285],{"type":25,"value":574},{"type":20,"tag":61,"props":1287,"children":1288},{"style":74},[1289],{"type":25,"value":1290},">> criteria ",{"type":20,"tag":61,"props":1292,"children":1293},{"style":89},[1294],{"type":25,"value":92},{"type":20,"tag":61,"props":1296,"children":1297},{"style":156},[1298],{"type":25,"value":159},{"type":20,"tag":61,"props":1300,"children":1301},{"style":74},[1302],{"type":25,"value":1303},";\n",{"type":20,"tag":61,"props":1305,"children":1306},{"class":63,"line":80},[1307,1311],{"type":20,"tag":61,"props":1308,"children":1309},{"style":68},[1310],{"type":25,"value":71},{"type":20,"tag":61,"props":1312,"children":1313},{"style":74},[1314],{"type":25,"value":77},{"type":20,"tag":61,"props":1316,"children":1317},{"class":63,"line":138},[1318,1323,1327,1332,1336,1341,1345,1349,1353,1357,1361],{"type":20,"tag":61,"props":1319,"children":1320},{"style":74},[1321],{"type":25,"value":1322},"    criteria ",{"type":20,"tag":61,"props":1324,"children":1325},{"style":89},[1326],{"type":25,"value":92},{"type":20,"tag":61,"props":1328,"children":1329},{"style":74},[1330],{"type":25,"value":1331}," Or.",{"type":20,"tag":61,"props":1333,"children":1334},{"style":100},[1335],{"type":25,"value":584},{"type":20,"tag":61,"props":1337,"children":1338},{"style":74},[1339],{"type":25,"value":1340},"(criteria, ",{"type":20,"tag":61,"props":1342,"children":1343},{"style":111},[1344],{"type":25,"value":114},{"type":20,"tag":61,"props":1346,"children":1347},{"style":89},[1348],{"type":25,"value":119},{"type":20,"tag":61,"props":1350,"children":1351},{"style":74},[1352],{"type":25,"value":124},{"type":20,"tag":61,"props":1354,"children":1355},{"style":127},[1356],{"type":25,"value":130},{"type":20,"tag":61,"props":1358,"children":1359},{"style":89},[1360],{"type":25,"value":210},{"type":20,"tag":61,"props":1362,"children":1363},{"style":74},[1364],{"type":25,"value":1365}," customerActive);\n",{"type":20,"tag":61,"props":1367,"children":1368},{"class":63,"line":167},[1369,1373],{"type":20,"tag":61,"props":1370,"children":1371},{"style":68},[1372],{"type":25,"value":71},{"type":20,"tag":61,"props":1374,"children":1375},{"style":74},[1376],{"type":25,"value":1377}," (customerEnterprise)\n",{"type":20,"tag":61,"props":1379,"children":1380},{"class":63,"line":342},[1381,1385,1389,1393,1397,1401,1405,1409,1413,1417,1421],{"type":20,"tag":61,"props":1382,"children":1383},{"style":74},[1384],{"type":25,"value":1322},{"type":20,"tag":61,"props":1386,"children":1387},{"style":89},[1388],{"type":25,"value":92},{"type":20,"tag":61,"props":1390,"children":1391},{"style":74},[1392],{"type":25,"value":1331},{"type":20,"tag":61,"props":1394,"children":1395},{"style":100},[1396],{"type":25,"value":584},{"type":20,"tag":61,"props":1398,"children":1399},{"style":74},[1400],{"type":25,"value":1340},{"type":20,"tag":61,"props":1402,"children":1403},{"style":111},[1404],{"type":25,"value":114},{"type":20,"tag":61,"props":1406,"children":1407},{"style":89},[1408],{"type":25,"value":119},{"type":20,"tag":61,"props":1410,"children":1411},{"style":74},[1412],{"type":25,"value":124},{"type":20,"tag":61,"props":1414,"children":1415},{"style":127},[1416],{"type":25,"value":317},{"type":20,"tag":61,"props":1418,"children":1419},{"style":89},[1420],{"type":25,"value":210},{"type":20,"tag":61,"props":1422,"children":1423},{"style":74},[1424],{"type":25,"value":1425}," customerEnterprise);\n",{"type":20,"tag":38,"props":1427,"children":1429},{"id":1428},"how-it-works",[1430],{"type":25,"value":1431},"How it works",{"type":20,"tag":21,"props":1433,"children":1434},{},[1435,1437,1443],{"type":25,"value":1436},"There are two parts to this working. The first combines the two separate predicates into a single OR lambda, taken care of by the ",{"type":20,"tag":57,"props":1438,"children":1440},{"className":1439},[],[1441],{"type":25,"value":1442},"Expression.OrElse",{"type":25,"value":1444}," method.",{"type":20,"tag":21,"props":1446,"children":1447},{},[1448],{"type":25,"value":1449},"That alone can't quite do what we need because the two expressions each have a separate parameter variable. The visitor replaces both of these with a new parameter we'll pass into the new Lambda expression we're creating that combines both conditions.",{"type":20,"tag":38,"props":1451,"children":1453},{"id":1452},"caveats",[1454],{"type":25,"value":1455},"Caveats",{"type":20,"tag":21,"props":1457,"children":1458},{},[1459,1461,1467],{"type":25,"value":1460},"The code, as it stands, creates an unbalanced tree. Ideally, the tree should be balanced to limit the depth LINQ providers must traverse to translate the query. I doubt you'll run into the dreaded ",{"type":20,"tag":57,"props":1462,"children":1464},{"className":1463},[],[1465],{"type":25,"value":1466},"Stack Overflow",{"type":25,"value":1468}," (the message, not the site), but if you do, that's why.",{"type":20,"tag":21,"props":1470,"children":1471},{},[1472,1474,1480],{"type":25,"value":1473},"An overload that takes an Enumerable of ",{"type":20,"tag":57,"props":1475,"children":1477},{"className":1476},[],[1478],{"type":25,"value":1479},"Expression\u003CFunc\u003CT, bool>>",{"type":25,"value":1481}," and produces a balanced tree is left as an exercise to the reader ",{"type":20,"tag":21,"props":1483,"children":1484},{},[1485],{"type":25,"value":1486},"[)amien",{"type":20,"tag":1488,"props":1489,"children":1490},"style",{},[1491],{"type":25,"value":1492},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":54,"searchDepth":80,"depth":80,"links":1494},[1495,1496,1497,1498,1499],{"id":40,"depth":80,"text":43},{"id":223,"depth":80,"text":226},{"id":492,"depth":80,"text":495},{"id":1428,"depth":80,"text":1431},{"id":1452,"depth":80,"text":1455},"markdown","content:blog:2021:or-expressions-in-linq.md","content","blog/2021/or-expressions-in-linq.md","blog/2021/or-expressions-in-linq","md","/blog/2021/or-expressions-in-linq/",638,0,[1510,1514,1518],{"title":1511,"date":1512,"url":1513},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":1515,"date":1516,"url":1517},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":1519,"date":1520,"url":1521},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[],1779224641879]