在linq中加入两个表问题

Join two tables issue in linq

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

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

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

但是当我加入另一个table时出现错误

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

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

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();

好的,所以你有一个 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;}
}

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

仅当您想对表或列使用非默认名称时,您才需要属性或流畅 API。

回到你的问题

Given an Id you want several properties of the Employee with this Id, inclusive several properties of the City that houses this Employee

您可以使用联接。但是,如果您使用 City 属性,那么 entity framework 足够聪明,可以了解需要哪个连接。代码看起来更自然 reader:

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。要获取这一位员工,请使用:

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,
        },
    });