【问题标题】:Join two tables issue in linq在 linq 中加入两个表问题
【发布时间】:2019-03-19 19:52:36
【问题描述】:

我正在开发一个用于报告和更新的门户网站.. 用于显示记录.. 我从控制器参数传递 id ..

如果我从一个表中获取数据,它工作正常.. 例如

Var employee = slp.urlt.where ( x=> x.Id == Id).ToList ();

但是当我加入另一个表时,它给出了错误

var result = from ut in slp.urlt
                     join ct in slp.Cities on ut.City equals ct.Id
                     where ut.Id == Id
                     select new
                     {
                         ut.R_Name_Enn,
                         ut.R_Name_Arr,
                         ut.R_Addr_Enn,
                         ut.R_Addr_Arr,
                         ct.Id,
                         ct.Name,
                         ct.Name_Arr
                     };

所以我检查了 sql .. 以下查询在 sql 中工作正常 ..

SELECT A.R_name_e,A.R_name_a,A.R_addr_e,A.R_addr_a,B.Id,B.Name,B.Name_ar FROM urlt A inner join City B on A.City = B.Id WHERE A.Id = 90000001

【问题讨论】:

标签: asp.net-mvc linq asp.net-mvc-5 entity-framework-6 linq-to-entities


【解决方案1】:

好的,所以你有一个 DbContextEmployeesCities。每个Employee 都住在City;每个City 是零个或多个Employees 的住所。显然是使用外键的简单一对多关系。

entity framework code-first conventions 之后,您将拥有类似于以下类的内容:

class City
{
    public int Id {get; set;}

    // every city Houses zero or more Employees:
    public virtual ICollection<Employee> Employees {get; set;}

    ... // other properties
}

class Employee
{
    public int Id {get; set;}

    // every Employee lives in exactly one City, using foreign key
    public int CityId {get; set;}
    public virtual City {get; set;}
}

class MyDbContext : DbContext
{
    public DbSet<City> Cities {get; set;}
    public DbSet<Employee> Employees {get; set;}
}

因为我遵循实体框架代码优先约定,实体框架将能够检测表和列以及城市和员工之间的一对多关系。

只有当你想为表或列使用非默认名称时,你才需要属性或流畅的 API。

回到你的问题

给定一个Id,您需要Employee 的几个属性 Id,包括容纳此的 City 的几个属性 Employee

您可以使用连接。但是,如果您使用City 属性,那么实体框架就足够聪明,可以了解需要哪个连接。代码对读者来说看起来更自然:

var queryEmployees = myDbcontext.Employees   // from the sequence of all Employees
    .Where(employee => employee.Id == Id)    // keep only the employee with this Id
    .Select(employee => new                  // from the remaining employees
    {                                        // make one new object with properties:
        NameEnn = employee.R_Name_Enn,
        NameArr = ut.R_Name_Arr,
        AddrEnn = ut.R_Addr_Enn,
        AddrArr = ut.R_Addr_Arr,            
        City = new                           // I chose to make a sub-property for city
        {                                     // if desired, you can flatten it.
            Id = employee.City.Id,
            Name = employee.City.Name,
            NameArr = employee.City.Name_Arr,
        },
    });

我预计只有一名员工使用此 ID。要获取这个 Employee 使用:

var fetchedEmployee = queryEmployees.FirstOrDefault();

或者如果你真的想要一个包含这个Employee的列表:

var fetchedEmployees = queryEmployees.ToList();

如果您真的认为联接更具可读性和可维护性 - 我对此表示怀疑 - 您可以使用内部联接获得相同的结果:

var queryEmployees = myDbcontext.Employees   // from the sequence of all Employees
    .Where(employee => employee.Id == Id)    // keep only the employee with this Id
    .Select(employee => new                  // join the remaining employees
    .Join(myDbcontext.Cities,                // with the sequence of Cities
    employee => employee.CityId,             // from each Employee take the CityId
    city => city.Id                          // from each City take the Id,
    (employee, city) => new                  // when they match 
    {                                        // make one new object with properties:
        NameEnn = employee.R_Name_Enn,
        NameArr = ut.R_Name_Arr,
        AddrEnn = ut.R_Addr_Enn,
        AddrArr = ut.R_Addr_Arr,            
        City = new
        {
            Id = city.Id,
            Name = city.Name,
            NameArr = city.Name_Arr,
        },
    });

【讨论】:

    【解决方案2】:

    我认为您忘记将查询放置或转换为列表。最后列出的地方。

    var result = (from ut in slp.urlt
                         join ct in slp.Cities on ut.City equals ct.Id
                         where ut.Id == Id
                         select new
                         {
                             ut.R_Name_Enn,
                             ut.R_Name_Arr,
                             ut.R_Addr_Enn,
                             ut.R_Addr_Arr,
                             ct.Id,
                             ct.Name,
                             ct.Name_Arr
                         }).ToList();
    

    【讨论】:

    • 出现错误,如用户代码未处理实体命令执行异常
    猜你喜欢
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多