Func运行查询和Expression Func之间的区别 [英] diffrence between running query with Func and Expression Func

查看:77
本文介绍了Func运行查询和Expression Func之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在互联网上搜索以发现Func和Expression Func之间的区别,我以某种方式明白了这一点,第一个只是一个函数获取数据,然后将其应用到内存中,而第二个将其翻译为sql并在数据库中运行它,在我运行以下两个查询之后:

I have been searching on internet to find out the difference between Func and Expression Func, somehow i got the point,the first one is just a function gets the data and then apply the function in memory but the second,translate it to sql and run it in the database,after i run this two queries:

 public IEnumerable<T> SelectAll(Expression< Func<T, bool>> predicate)
    {         
        return table.Where(predicate).ToList();
    }

     public IEnumerable<T> SelectAll(Func<T, bool> predicate)
    {

        return table.Where(predicate).ToList();

    }

我将断点放在返回上,第一个返回12行,第二个返回1200行,谓词为:

i put the breakpoint on return,for first one it returns 12 rows,for second it returns 1200 row,the predicate is :

s=>s.id="12345"

第二个,在获取数据后应用谓词,我的问题是,在处理DB时,我们通常应该使用func表达式吗?

the second one,apply the predicate after it get the data,my question is,we usualy should use the expression func when we deal with DB?

推荐答案

我的问题是,在处理数据库时,通常应使用func表达式吗?

my question is,we usualy should use the expression func when we deal with DB?

是的,因为您希望向SQL Server提供尽可能多的信息,以便它可以优化数据库访问,并且您希望最小化从SQL Server返回的数据(删除所有不必要的数据,并对数据进行汇总可以很容易地由SQL Server聚合),因为将数据从SQL Server移至.NET计算机甚至是一项工作".

Yes, because you want to give as much information to the SQL server so that it can optimize the database access and you want to minimize the data that is returned from the SQL Server (removing all the unnecessary data, and aggregating the data that can be easily aggregated by the SQL Server) because moving data from the SQL Server to the .NET machine is even that a "work".

很显然,如果在SQL中很难完成某些事情(例如字符串操作),则可以将其移至.NET.

Clearly if something is very difficult to do in SQL (like string manipulation), then moving it to .NET is acceptable.

请注意,使用LINQ,您对结果查询没有很好的控制,它很容易变成多级野兽",也无法真正确定查询的哪一部分将在SQL Server上执行以及哪一部分将在本地执行(例如,EF Core通常在本地执行GROUP BY),也无法访问SQL的许多高级功能(例如,所有分区方法).

Note that with LINQ you don't have fine control on the resulting query, that can easily become a "multi-level-beast", nor you can be really sure which part of the query will be executed on the SQL server and which part will be executed locally (EF Core for example often executes GROUP BY locally), nor can access many advanced features of SQL (all the partitioning methods for example).

这篇关于Func运行查询和Expression Func之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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