仅在非空的情况下才在where子句中使用变量?一种动态where子句?

Mar*_*tin 6 linq dynamic where-clause

是否可以在where(LINQ)中包含一个子句,但前提是它的"NOT"为空?

  where c.Code == sCode && p.Code == code
Run Code Online (Sandbox Code Playgroud)

在这种情况下,变量(标准c#)被称为代码...如果它的非空,那么上面的地方很棒..但如果它是空的那么我不想在其中包含

  where c.Code == sCode
Run Code Online (Sandbox Code Playgroud)

在SQL中它的完成就像这样

       AND ( ( @code = '' )
                  OR ( @code <> ''
                       AND C.Code = @code
                     )
Run Code Online (Sandbox Code Playgroud)

Gre*_*reg 10

where c.Code == sCode && (string.IsNullOrEmpty(code) || p.Code == code)
Run Code Online (Sandbox Code Playgroud)

这是标准的LINQ,我不知道它是否适用于LINQ to SQL.

编辑:这通过短路评估工作

如果c.Code == sCode为false,则停止计算并返回false,
否则,如果string.IsNullOrEmpty(code)为true,则停止计算并返回true,
否则返回p.Code == code