本文介绍了性能比较的问题 - 在.Count之间的MVC视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我用几页的 .Count之间 A 的foreach 循环中。

  @model的ICollection<项目>
的foreach(在型号VAR项)
{
    @ item.Flight.FlightReservations.count
}

由于延迟加载的EF使往返于数据库这一点。

现在我想要使用此或LINQ版本来解决这个问题:
包括(List.Flight.FlightReservations)

这样做可以加载我dbSet甚至更长的时间比的foreach 往返

如何能只有1对象的I'负荷'部分?

我想用 context.Items.Single(P => p.id == ID).INCLUDE(.....)
所以我只有完全加载项目1项。

或者任何其它的解决方案?
(一种强制负载 item.List.item2.List 内部控制器)

任何建议,欢迎:)谢谢

修改:现在用

其中(..)ToDictionary(项目=>的项目,项目= GT; item.Flight.FlightReservations.Count);
还注意到添加索引到我的聚集索引表帮一点。
仍然缓慢,虽然

 变种F = _pr.FindBy(duifid);
VAR的结果=(从f.IngeschrevenVluchten p
              选择新的prestatieModel
              {
                 Eindpos = p.Eindpositie,
                 地区= p.Vlucht.Locatie.Naam,
                 AantalInschrijvingen = p.Vlucht.Inschrijvingen.Count(),
                 Vlucht = p.Vlucht
              });

该查询执行速度非常快,使得的IEnumerable<模型与GT; 。不过还是它加载很慢,一旦发送到视图。

 返回PartialView(结果);


解决方案

下面是一些提示,EF的速度:

1使用。之前的实体框架可以执行查询或更改保存到数据源,必须生成一组映射视图来访问数据库。因为映射视图生成是在执行第一个查询的总成本的一部分显著,实体框架允许您pre-生成映射的观点,包括他们在编译的项目。

2 - 不要创建新实例的DbContext 每一个地方,而不是使用,IOC 创建和维护的DbContext 实例。

3。如果你只愿意显示数据转实体只读场景在查询大量的实体时,tracking.This可能会导致更好的性能。

  VAR someData = context.SomeEntity.Where(...)AsNoTracking()了ToList()。

4-使用包含了ToList()方法从数据库中获取数据时,减少往返数据库。

  context.Items.Single(P => p.id == ID).INCLUDE(T => t.List1.Select(L => l.List2) ).ToList();

5如果你使用的数据量巨大寻呼

  VAR列表= context.Items.AsNoTracking()选择(X => x.Content)。.Skip(15)。取(15).ToList();

For several pages I use a .Count inside a foreach loop

@model ICollection<Item>
foreach(var item in Model)
{
    @item.Flight.FlightReservations.count
}

Because of lazy loading the EF makes a round trip to the database for this.

Now I want to fix this by using this or linq version:Include("List.Flight.FlightReservations")

Doing this makes loading my dbSet take even longer than those foreach round trips

How can I 'load' parts of only 1 object?

I would like to use context.Items.Single(p => p.id == id).Include(.....)So I only load 1 item fully.

Or any other solutions for this?(A way to force load item.List.item2.List inside controller)

Any suggestions are welcome :) Thanks

EDIT : now using

Where(..).ToDictionary(item => item, item => item.Flight.FlightReservations.Count);Also noticed adding an index to my 'clustered index' table helped a little.Still slow though

var f = _pr.FindBy(duifid);
var result = (from p in f.IngeschrevenVluchten
              select new PrestatieModel
              {
                 Eindpos = p.Eindpositie,
                 Locatie = p.Vlucht.Locatie.Naam,
                 AantalInschrijvingen = p.Vlucht.Inschrijvingen.Count(),
                 Vlucht = p.Vlucht
              });

This query executes very fast, making a IEnumerable<Model>. But still it loads very slow once sent to the view.

return PartialView(result);
解决方案

Here is some tips to speed of EF:

1- Use Pre-Generated Mapping Views. Before the Entity Framework can execute a query or save changes to the data source, it must generate a set of mapping views to access the database. Because mapping view generation is a significant part of the overall cost of executing the first query, the Entity Framework enables you to pre-generate mapping views and include them in the compiled project.

2- Do not create new instance of DbContext every where instead use Ioc for creating and maintaining DbContext instance.

3- If you only want display data then turn of entity tracking.This may result in better performance when querying for large numbers of entities in read-only scenarios.

var someData = context.SomeEntity.Where(...).AsNoTracking().ToList();

4- Reduce round trip to database when getting data from database by using Include and ToList() method.

context.Items.Single(p => p.id == id).Include(t => t.List1.Select(l => l.List2)).ToList();

5- If you have huge amount of data use Paging.

var list = context.Items.AsNoTracking().Select(x => x.Content).Skip(15).Take(15).ToList();

这篇关于性能比较的问题 - 在.Count之间的MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:55