Linq2SQL “or/and” operators (ANDed / ORed conditions)

前端 未结 2 1600
悲哀的现实
悲哀的现实 2020-12-12 01:11

Let\'s say we need to apply several conditions to select from a table called \"Things\" (unknown count and nature)

if conditions are known, we can write



        
2条回答
  •  有刺的猬
    2020-12-12 01:46

    For OR, you have two choices:

    • use Union/Concat
    • write the Expression in code

    The second is closer to the .Where(x => {a} || {b}).

    If you are using LINQ-to-SQL, you can use Expression.Invoke to combine multiple separate lambda expressions (see this answer) - however, this isn't supported in Entity Framework. In EF, you have to build the entire expression as a single block, using Expression.OrElse; for example here or here.

提交回复
热议问题