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

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

问题描述

代码1

我在查询中使用了两个where 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 where条件

I have using && condition without using two where condition

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







  • 什么是 code1 code2 之间的差异????


    • 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;和以及实体框架中的条件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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