简体   繁体   English

LINQ 中哪种方法更好?

[英]Which approach is better in LINQ?

I am using linq in c# and I have a quick question.我在 c# 中使用 linq,我有一个简单的问题。

I have shown very minimum amount of code here, in real-time there are also some sorting operations.我在这里展示了非常少量的代码,实时还有一些排序操作。 I would like to know from below which approach should I use??我想从下面知道我应该使用哪种方法?

Approach 1方法一

 public class UserDetails
    {
        private dbContext db = new dbContext();
        public List<User> ActiveUser()
        {
          return db.Users.Where(m => m.Active == true).ToList();                   
        }
        public List<User> InActiveUser()
        {
          return db.Users.Where(m => m.Active == false).ToList();                   
        }
       
    }

Approach 2方法二

 public class UserDetails
    {
       List<Defect> allUser = new db.Users.ToList();
        public List<User> ActiveUser()
        {
          return allUser.Where(m => m.Active == true).ToList();                   
        }
        public List<User> InActiveUser()
        {
          return allUser.Where(m => m.Active == false).ToList();                   
        }
       
    }

There are more than 20 methods which are fetching the data, every method is fetching data from same table with different where condition.有20多种获取数据的方法,每种方法都是从同一个表中获取不同where条件的数据。 My question is Should I create dbContext and then use separate query in every method ( Approch 1 ) OR Should I create one List and fetch all data and filter it in method itself using where condition.我的问题是我应该创建 dbContext 然后在每个方法中使用单独的查询(方法1 )还是我应该创建一个 List 并获取所有数据并使用 where 条件在方法本身中过滤它。 ( Approch 2 ) (接近 2 )

It depends on the circumstances.这取决于具体情况。 In general, I would prefer approach 1 as you usually can't predict how many users are in your database and you will pull all users into the memory which will cause a big memory overhead.通常,我更喜欢方法 1,因为您通常无法预测数据库中有多少用户,并且会将所有用户拉入内存,这将导致大量内存开销。 Also for big amounts of users you will take advantage of database optimisations like indexes and query execution plans.同样对于大量用户,您将利用数据库优化,如索引和查询执行计划。

If we are talking about a small amount of users approach 2 might be the more performant one as you reduce the amount of roundtrips to the database.如果我们谈论的是少量用户,则方法 2 可能是性能更好的方法,因为您减少了到数据库的往返次数。 But next to the memory overhead it also has other issues like caching the users and missing database updates.但除了内存开销之外,它还存在其他问题,例如缓存用户和丢失数据库更新。

However, I'd almost always prefer approach 1 as it's good practice to do most of the filtering and sorting work on the database as it is optimized for doing this kind of things.但是,我几乎总是更喜欢方法 1,因为在数据库上执行大部分过滤和排序工作是一种很好的做法,因为它已针对此类操作进行了优化。 With approach 2 you might get into trouble as your user base grows over time and it will be hard to track down caching or memory issues.使用方法 2,您可能会遇到麻烦,因为您的用户群会随着时间的推移而增长,并且很难追踪缓存或内存问题。 Also the difference between one and two database roundtrips is mostly neglegible if you don't have a really bad connection or do it many times.此外,如果您的连接不是非常糟糕或多次进行,则一次和两次数据库往返之间的差异几乎可以忽略不计。

Never load unnecessary data.永远不要加载不必要的数据。 In the Approach 2 you load an unnecessary amount of data.在方法 2 中,您加载了不必要的数据量。

我个人采用第一种方法,因为在第一种方法中,过滤数据是从数据库中获取的,而在第二种方法中,所有数据都被获取,然后过滤是在框架级别而不是数据库级别完成的,所以在第一种方法中我们可以得到性能优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM