Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何优化linq查询以提高速度?_C#_Wpf_Linq - Fatal编程技术网

C# 如何优化linq查询以提高速度?

C# 如何优化linq查询以提高速度?,c#,wpf,linq,C#,Wpf,Linq,我有一个linq查询,它将所有记录从customer表提取到一个可观察的集合,如下所示: customerList = new ObservableCollection<customer>(dbContext.customers); dgRecords1.ItemsSource = customerList; customerList=新的ObservableCollection(dbContext.customers); dgRecords1.ItemsSource=客户列表;

我有一个linq查询,它将所有记录从customer表提取到一个可观察的集合,如下所示:

customerList = new ObservableCollection<customer>(dbContext.customers);
dgRecords1.ItemsSource = customerList;
customerList=新的ObservableCollection(dbContext.customers);
dgRecords1.ItemsSource=客户列表;
该列表绑定到数据网格。 customer表包含许多字段,将近100个。但我只显示datagrid上的几个字段。我的问题是

使用linq查询仅从数据库中引入选定字段是否会提高客户屏幕的速度

我需要从这个列表中筛选和删除记录


哪种方法是在可观察的集合中选择几个字段的最佳方法,有人能给出一些示例linq查询吗?

如果您只选择所需的列而不是所有列,那么性能会有所提高。您可以使用来实际计算差异。最好只从数据库中选择所需的列,而不是全部


<>您可以考虑使用<代码>跳过和 > 实现分页。您可能会看到:

优化速度的提示:

  • 减少列数可以减少所需的带宽
  • 减少行数,但引入分页,将带宽减少了(通常)
  • 在LINQ to SQL中关闭更改跟踪和身份管理(例如
    ObjectTrackingEnabled
    )将减少数据后处理的开销
  • 使用预编译查询有时有助于减少预处理开销
。。。但坦率地说,当我们遇到这个问题时,我们“一劳永逸”地解决了它,写下了“整洁”一词,并开始老一套:

var list = connection.Query<CustomerViewModel>(
    "select {some specific cols} from Customers").ToList();

这将减少所有不必要的开销,在您只想显示数据时非常理想;此外,参数化层和物化层经过了非常优化(使用策略缓存,以获得最佳性能)。

很明显,限制结果集中的列数将减少需要传输的数据量。您可以尝试的另一件事是分页。或预取。。。
class CustomerViewModel {
   public int Id {get;set;}
   public string Name {get;set;}
   // ...
}