带Lambda表达式中的Join的where子句 [英] Where clause with Join in lambda expression

查看:248
本文介绍了带Lambda表达式中的Join的where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Lambda表达式用于带有Join的Where子句.这是我的查询

I am using Lambda expression for Where Clause with Join. Here is my query

 var ActiveImages = db.tbl_Advertise
    .Where(i => i.IsVisible == true)
    .Join(db.tbl_ShopMast.Where(i => i.IsVisible == true && i.fk_userID == userid),
        i => i.fk_shop_id,
        j => j.ShopID,
        (i, j) => new { Advertise = i, Shop = j})
    .ToList();

或者我甚至可以将查询查询为:

or I can even right this query as :

var ActiveImages = db.tbl_Advertise
    .Join(db.tbl_ShopMast.Where(i => i.IsVisible == true && i.fk_userID == userid),
        i => i.fk_shop_id,
        j => j.ShopID,
        (i, j) => new { Advertise = i, Shop = j})
    .ToList()
    .Where(i=>i.Advertise.IsVisible == true);

哪个更快?尽管我注意到两者都给出了相同的输出,但是哪种方法正确?

Which one works faster? Although I have noticed both giving same output, but which way is correct?

推荐答案

对于第一个查询,where子句在数据库服务器上执行,而对于第二个查询,它在客户端计算机上执行.因此,使用第一个查询:

With your first query, the where clause is executed on the database server, while with your second query it is executed on your client machine. Because of that, with the first query:

  • 数据库服务器要做更多的工作;
  • 客户端计算机的工作量较少;
  • 更少的数据从服务器传输到客户端;

,第二个查询则相反.很难说哪个会更快.在大多数情况下,您的第一个查询会运行得更快,因此更可取,但是我见过类似第二个查询的查询会运行得更快的情况.

and with the second query it's just the opposite. It's hard to say which will be faster. Most of the time your first query would run faster and thus is preferred, but I've seen scenarios where queries like your second one runs faster.

这篇关于带Lambda表达式中的Join的where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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