Linq 可查询什么更快:直接访问 table 还是通过对象层次结构?
Linq iqueryable What is faster: Accessing table directly or through object hierarchy?
假设我有 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()
哪里性能比较好?
这些表达是否相同?
没有您的数据集和上下文,我们无法告诉您哪个更适合您的场景。
我建议您做自己的测试场景:
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);
假设我有 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()
哪里性能比较好? 这些表达是否相同?
没有您的数据集和上下文,我们无法告诉您哪个更适合您的场景。
我建议您做自己的测试场景:
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);