【问题标题】:Entity Framework, how to use IQueryable with multiple where are translated to SQL?实体框架,如何将 IQueryable 与多个 where 转换为 SQL 一起使用?
【发布时间】:2017-12-18 23:58:18
【问题描述】:

这里的一切都是IQueryable

  1. 这 2 个示例是否生成相同的 SQL 查询?
  2. 添加多个 Where 被转换为 SQL 为 AND ?
  3. 有没有办法添加多个Where连接为OR?

示例 1:

client = client.Where(c => c.FirstName.StartsWith("F")); 
client = client.Where(c => c.LastName.StartsWith("T")); 
return client.ToList();

示例 2:

client = client.Where(c => c.FirstName.StartsWith("F") AND c.LastName.StartsWith("T")); 
return client.ToList();

【问题讨论】:

    标签: entity-framework linq linq-to-sql entity-framework-4


    【解决方案1】:

    多个 where 子句有效。相当于:

    client = client.Where(c=> c.FirstName.StartsWith("F") && c.LastName.StartsWith("T"));
    

    在您的情况下,它将在 .ToList() 调用中发送到 SQL。 它将被执行的其他情况包括: .Any()、.First()/.Last()/.FirstOrDefault()/etc.、.Count()。

    【讨论】:

      【解决方案2】:

      试用代码

      client = client.Where(c => (c.FirstName.StartsWith("F") && c.LastName.StartWith("T"))).ToList();
      

      或使用的条件

       client = client.Where(c => (c.FirstName.StartsWith("F") || c.LastName.StartWith("T"))).ToList();
      

      【讨论】:

        【解决方案3】:

        在 LINQ 中添加多个条件有多种方法。请点击here!了解更多信息。谢谢。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多