&& 之间的区别以及实体框架查询中的 where 条件 [英] Difference between && and where condition in entity framework query

查看:25
本文介绍了&& 之间的区别以及实体框架查询中的 where 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码 1

我在查询中使用了两个 where 条件

I have using two where condition in my query

 dbContext.Projects.Where(p=>p.ProjectId!=ProjectId).Where(p=>p.Name==Name)
 .SingleOrDefault();

<小时>

代码 2

我已经使用 &&条件不使用两个 where 条件

I have using && condition without using two where condition

  dbContext.Projects.Where(p=>p.ProjectId!=ProjectId &&  
  p.Name==Name).SingleOrDefault();

<小时>

  • code1code2 有什么区别????

    • What is the difference between code1 and code2????
    • 两个查询都返回相同的值.但我不知道有什么区别.请给我解释一下,哪个更好.为什么?

      The both queries are return same value. but i don't know the differences. Please explain to me, which one is better. and why?

      推荐答案

      如果您在 LinqPad 中打开您的查询,您将看到两个查询

      If you open your query in LinqPad you will see that both queries

      dbContext.Projects.Where(p=>p.ProjectId!=ProjectId).Where(p=>p.Name==Name)
      
      dbContext.Projects.Where(p=>p.ProjectId!=ProjectId && p.Name==Name);
      

      将导致两者

      SELECT [...]
      FROM [...]
      WHERE p.ProjectId <> someId AND p.Name = 'something'
      

      性能和 SQL 查询结构都没有区别.

      There is no difference neither in performance nor in SQL query structure.

      这篇关于&amp;&amp; 之间的区别以及实体框架查询中的 where 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆