【问题标题】:Linq to Entities Where clause compare value that can be int or stringLinq to Entities Where 子句比较可以是 int 或 string 的值
【发布时间】:2016-12-01 06:28:24
【问题描述】:
我有一个下拉列表,可以提供数字或单词 ANY。我需要创建一个包含可以模仿以下 SQL 的 WHERE 子句的 LINQ SELECT:
var p varchar2(3);
select ... from ...
where (
( (:p = 'ANY') and id in (select distinct id from Ids) )
or
(:p='1' and id = 42)
)
ps:我将使用表达式树来处理 OR 方面:-)
【问题讨论】:
标签:
c#
linq-to-entities
where-clause
【解决方案1】:
像这样吗?
string input = /***/
var result = Context.Entities
.Where(ent => (input == "ANY"
&& Context.UserIds.Select(usr => isr.Id)
.Distinct()
.Contains(ent.Id))
|| (input == "1" && ent.Id == 42))
.Select(ent => /***/);
免责声明:凭记忆编写,可能包含编译时错误(拼写错误等)