Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net EntityFramework Core在联接后获得多个结果_Asp.net_Entity Framework_Asp.net Core_.net Core_Entity Framework Core - Fatal编程技术网

Asp.net EntityFramework Core在联接后获得多个结果

Asp.net EntityFramework Core在联接后获得多个结果,asp.net,entity-framework,asp.net-core,.net-core,entity-framework-core,Asp.net,Entity Framework,Asp.net Core,.net Core,Entity Framework Core,数据库有表、机器、轮子和特征。 1车辆记录包括4个车轮记录和几个特征。在数据库中,它看起来像这样: 我想得到关于汽车的最后5个条目,包括一整套参数数据和车轮信息。我想在一个请求中获得所有数据 var result = (from Machines in db.Machines.Where(x => x.param == 2) orderby Machines.ID descending join wheel in db.Wheels on Ma

数据库有表、机器、轮子和特征。 1车辆记录包括4个车轮记录和几个特征。在数据库中,它看起来像这样:

我想得到关于汽车的最后5个条目,包括一整套参数数据和车轮信息。我想在一个请求中获得所有数据

 var result = (from Machines in db.Machines.Where(x => x.param == 2) orderby Machines.ID descending
                      join wheel in db.Wheels on Machines.ID equals wheel.MachineId
                      join param in db.Characteristcs on Machines.ID equals param.MachineId
                      select new { Machines, param, wheel } 
                      ).Take(5).ToList();

但是它只返回一个控制盘记录和一个特征。

我认为您应该首先查看您的模型是否定义了所需实体的导航属性:

public class Machine {

  public int ID {get;set;}
  //...
  //Navigation Properties
  public IList<Wheel> Wheels {get;set;}
  public IList<Charateristic> Charateristics {get;set;}

}
Include
方法有助于构建从相关表中获取所需数据的查询

如果您还没有定义导航属性(我强烈建议您这么做),您可以这样做

  • 在SQL Server中,创建机器与控制盘和特征表的关系
  • 重新运行scaffolding dbcontext以更新EF dbcontext和模型
  • 使用Include linq。 var result=db.Machines.Where(x=>x.Id==2)。Include(x=>x.Wheels)。Include(x=>x.Characteristics)

    在EF Core中,您可以参考和了解更多详细信息

    这是我的工作演示:

    模型

    控制器


    不要使用连接。在实体之间使用适当的关系,并让ORM生成连接。这就是它的工作你的模型类代码和实际结果的屏幕截图是什么?我根据你的截图和代码为测试创建了一个演示,它得到了预期的结果。您可以参考我下面的答复。@TiyebM*“在不需要预定义导航规则的情况下创建并执行查询的想法很强烈,应该由框架本身来解决;“导航属性的概念要强烈得多,而且总是比手动连接更受欢迎,尤其是对于像本例中这样表示关系的东西。因为它们不仅仅支持
    包含
    。当在查询中使用时,它们允许ORM创建连接,并允许开发人员使用更自然的面向对象方法集中查询逻辑。联接是RDBMS(SQL)特性,它们存在于LINQ中的唯一原因是LINQ to对象需要它们来高效地实现相关操作。EF Core根本不需要它们-相关的
    ,其中
    DefaultIfEmpty
    足以在需要时生成适当的SQL联接。一般来说,EF Core或EF6都是不错的答案,但EF Core(到目前为止,将在下一个版本中看到会发生什么)始终有自己的LINQ查询转换处理方法。这里的“小”问题是EFC 3.x-5.x中不支持组联接,因此第二部分根本不起作用(运行时异常)。这会将整个集合(表)加载到内存中,以获取所需的5项。一般不要使用这样的代码(它只适用于字段很少的小表),根据使用的EFC版本,有更好的解决方案。
    var query= context.Machines.Include(m=> m.Wheels)
                               .Include(m=>m.Charateristics)
                               .Where(x => x.param == 2)
                               .Take(5)
                               .ToList();
    
    var query=  from m in context.Machines
                join c in context.Charateristics on m.Id equals c.MachineId into gc
                join w in context.Wheels on m.Id equals w.MachineId into gw
                select new {Machine= m, Wheels= gw, Charateristics=gc };
    
    var result= query.Take(5).ToList();
    
    public class Machines
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public string Color { get; set; }
        public int Weight { get; set; }
        public int Param { get; set; }
    }
    public class Characteristics
    {
        public int Id { get; set; }
        public string Parameter { get; set; }
        public string Value { get; set; }
        public string Description { get; set; }
    
        public int MachineId { get; set; }
    }
    public class Wheels
    {
        public int Id { get; set; }
        public int Radius { get; set; }
        public int Weight { get; set; }
        public int MachineId { get; set; }
    }
    
    public IActionResult GetCars()
    {
            var collection = (from Machines in _context.Machines.Where(x => x.Param == 2)
                          join wheel in _context.Wheels on Machines.Id equals wheel.MachineId
                          join param in _context.Characteristics on Machines.Id equals param.MachineId
                          select new { Machines, param, wheel }
                      ).ToList();
    
            //get the last 5 entries about cars
            var result= collection.Skip(Math.Max(0, collection.Count() - 5)).ToList();
            return new JsonResult(result);
    }