繁体   English   中英

使用LINQ实体框架快速加载

[英]Eager Loading with Entity Framework, LINQ

我正在研究ASP.NET MVC项目。 我正在使用EF代码优先方法。 我有3个课程,分别是:

public class A
{
    public int AID {get;set;}
    public string A1 {get;set}
    public string A2 {get;set}

    public virtual List<B> Bs {get;set;}
}

public class B
{
    public int BID {get;set;}
    public string B1 {get;set}
    public string B2 {get;set}

    public AID {get;set}
    public virtual A A {get;set}

    public virtual List<C> Cs {get;set;}
}

public class C
{
    public int CID {get;set;}
    public string C1 {get;set}
    public string C2 {get;set}

    public BID {get;set}
    public virtual B B {get;set}
}

我只想基于A1 = 4的B类选择C类的C1属性。 我尝试使用:

var result = db.C.select(x=>x.C1).Include(x=>B).where(x=>x.A.equals(4))

我很困惑,不知道如何执行linq查询。 另外我不确定是继续使用急切加载还是继续使用其他方法。

请上师帮我一下好吗?

尝试这个:

var result = db.C
    .Where(c => c.B.A.A1 == 4)
    .Select(c => c.C1)
    .ToList()

您不必在这里使用Eager加载( Include ),因为结果中不Include任何嵌套实体。

急切的加载用于解决SELECT N + 1问题。 当您检索父实体并想要遍历其子实体时,会出现此问题。 这导致对数据库发出N + 1个请求。

以下是代码示例以说明:

没有急切的加载

var carList = db.Cars.ToList(); //this will create one request to the database to retrieve all cars

foreach(var car in carList)
{
    foreach(var wheel in car.Wheels) //this line will create another request to the database to retrieve wheels for specific car
    }
        Console.Write("Car = {0}, Wheel = {1}", car, wheel);
    }
}
//Total requests: 1 to get car list + N requests to retrieve wheels where N - total number of cars

急切加载

var carList = db.Cars.Include(x => x.Wheels).ToList(); //this will create one request to the database to retrieve all cars together with information about wheels

foreach(var car in carList)
{
    foreach(var wheel in car.Wheels) //this line won't create extra request to the database because this data has been already loaded using eager loading
    }
        Console.Write("Car = {0}, Wheel = {1}", car, wheel);
    }
}
//Total requests: 1 to get car list with all wheel information

默认情况下,EF使用延迟加载,这意味着除非您要求,否则您不会获得延迟加载。 使用ToList()或ToArray()使EF对SQL执行查询并将这些对象放入内存中的实体。 如果需要,您可以执行原始SQL查询,例如YourDbContext.SqlQuery<T>("select * from ...")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM