问题描述
我有以下查询:
baseQuery = Buildings.Where(
i => i.Apartment != null
&& i.Apartment.Manager != null
&& i.Apartment.Manager.Name == Username);
我想我收到错误,因为我在这里执行三个单独的查询,所以这个操作会变得太慢任何方式我可以优化这个检查?
I think I'm getting the error because I'm performing three separate queries here, so this operation is going to be super slow. Any way I can optimize this check?
编辑:我从resharper得到可能的多个枚举的IEnumerable。
I get "Possible Multiple Enumeration of IEnumerable" from resharper.
推荐答案
假设 ContextProvider.Context.Inspections
是一个 DbSet
或 IQueryable
,这不会导致多次枚举IEnumerable。表达式将被翻译成SQL查询并在服务器上执行。您可以通过在您的行之后放置一个断点来检查这个,并将鼠标悬停在 baseQuery
上以查看生成的SQL。
Assuming that ContextProvider.Context.Inspections
is a DbSet
or an IQueryable
, this will not result in multiple enumerations of an IEnumerable. The expression will be translated into an SQL query and executed on the server. You can check this by putting a breakpoint after your line, and hovering over baseQuery
to see the generated SQL.
看起来像一个来自resharper的假阳性。
Looks like a false positive from resharper.
这篇关于IEnumerable可能的多个枚举检查子对象/子孙对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!