【问题标题】: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 => /***/);
    

    免责声明:凭记忆编写,可能包含编译时错误(拼写错误等)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 2011-11-25
      • 2011-10-18
      相关资源
      最近更新 更多