在呈现我的视图之前急于加载 Linq to Entity 查询会减慢速度吗?

Will eager loading a Linq to Entity query before rendering my view slow things down?

呈现我的视图时,我需要从我的视图模型中获取位置数据(long/lat 坐标数组)。我将使用 Javascript 读取这些值并将它们绘制在 Google 地图中。目前我正在获取包含位置数据的结果,但我不确定如何有效地从结果中获取位置数据并将它们放入我的视图模型中的另一个 属性 中。这应该是急切加载,延迟加载等?我是 Linq 和 EF 的新手。

我有一个看起来像这样的视图模型

public class YogaSpaceListViewModel
{
    public IPagedList<YogaSpaceResults> YogaSpaces { get; set; }
    // I need to put all LocationPoints data from the query results into a collection here
    //public some collection here LocationResults { get; set; }
}

仅供参考 IPagedList 继承自 IEnumerable。

当我获取结果时,这里是我的查询。我将结果放入匿名类型 'YogaSpaceResults',您可以看到它包含我想存储在我的视图模型中的 'LocationPoints' 数据。

var events = (from u in context.YogaSpaceEvents
    orderby u.YogaSpace.Address.LocationPoints.Distance(myLocation)
    where
        (u.DateTimeScheduled >= classDate) &&
        (u.YogaSpace.Address.LocationPoints.Distance(myLocation) <= 8047)
            select new YogaSpaceResults 
            {   
                LocationPoints = u.YogaSpace.Address.LocationPoints,
                Title = u.YogaSpace.Overview.Title,
                Summary = u.YogaSpace.Overview.Summary,
                Date = u.DateTimeScheduled
            }).ToPagedList(page, 10);

在我看来我正在做这样的事情

@foreach (var space in Model.YogaSpaces)
    {
        <div>
            <h4>@space.Title</h4>
            <div>
                @space.Summary
                <br/>
                @space.Date
            </div>
        </div>
        <hr />
    }

I'm not sure how to efficiently get the location data out of the results and place them into another property in my view model. Should this be eagerly loaded, lazy loaded, etc.?

当您执行 ToPagedList 时,它们已经从数据库中加载,因此您无需加载它们,一切都将在内存中完成

你可以有一些非常简单的东西,比如

public IEnumerable<LocationPoint> LocationResults {
    get{
        return YogaSpaces.Select(ys => ys.LocationPoints)
    }
}

顺便说一句,您似乎已经以高效的方式从数据库中获取数据(您使用单个查询在视图模型中投影所需的属性,这比延迟加载或预加载更有效)因此,您可以轻松保留当前代码。

请注意,如果您发现您的查询速度太慢,可能是由于这种情况缺少空间索引

(u.YogaSpace.Address.LocationPoints.Distance(myLocation) <= 8047)