【问题标题】:Linq iqueryable What is faster: Accessing table directly or through object hierarchy?Linq iqueryable 什么更快:直接访问表还是通过对象层次结构访问表?
【发布时间】:2018-05-18 08:57:39
【问题描述】:

假设我有 2 个相关表: 候选人 - 有通知列表 所以问题是:

Candidates.SelectMany(c=>c.Notifications.Where(...).ToList())

或类似的东西

var candIds = ...//int array of candidates that i need
Notifications.Where(n=>candId.Contains(n.CandidateId) && ...).ToList()

哪里的性能更好? 那些表达是一样的吗?

【问题讨论】:

  • "哪里的性能更好?"为什么不试试看呢?只需测量。反正关于性能的几乎每一个问题,你可以看看Eric Lipperts Blog
  • 你的两个查询是伪代码,所以不一样。甚至不清楚它们是如何相关的。你不能提供一个更好的例子吗?我的建议是:让数据库做优化,不要在查询中间使用ToList

标签: c# linq entity hierarchy


【解决方案1】:

如果没有您的数据集和上下文,我们无法告诉您哪个更适合您的场景。

我建议做你自己的测试场景:

var candIds = ...//int array of candidates that i need
var watch = new Stopwatch(); 
watch.Start();
Candidates.SelectMany(c=>c.Notifications.Where(...).ToList())
watch.Stop();
Console.WriteLine("Time Elapsed {0} ms", watch.Elapsed.TotalMilliseconds);
watch.Restart();
Notifications.Where(n=>candId.Contains(n.CandidateId) && ...).ToList();
watch.Stop();
Console.WriteLine("Time Elapsed {0} ms", watch.Elapsed.TotalMilliseconds);

【讨论】:

    猜你喜欢
    • 2018-07-04
    • 2011-02-08
    • 1970-01-01
    • 2022-08-06
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    相关资源
    最近更新 更多