2015-07-19 53 views
1

我有2个表Job_tableEmployee_table。我想在Employee_table的索引视图中显示来自Job_tableemp_id和来自Employee_table的对应emp_name从不同的表中取数据并将其显示在查看索引中

对于这个问题,我创建一个ViewModel EmployeeViewModel并将我的行为结果的索引视图与IEnumerable<EmployeeViewModel>绑定。对于EmployeeViewModel的定义是这样的:

public class EmployeeViewModel 
{ 
    public int EmployeeId { get; set; } 
    public string EmployeeName { get; set; } 
    public string JobName { get; set; } 
    //..Other memberVariables.. 
} 

我的模型:

public class Employee 
    { 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int EmployeeId { get; set; } 
     public string EmployeeName { get; set; } 
     public string Address { get; set; } 
     public virtual ICollection<Job> Jobs { get; set; } 
    } 

,工作台,更名为工作为自己的方便:

public class Job 
{ 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int JobId { get; set; } 
    public string JobName { get; set; } 
    public JobCategory JobCategory { get; set; } 
    public int EmployeeId { get; set; } 
    public virtual ICollection<Employee> Employees { get; set; } 
} 

在我的索引行动,我通过连接两个表创建结果集,将其绑定到IEnumerable<EmployeeViewModel>并将其作为模型传递给视图。该视图应接收IEnumerable<EmployeeViewModel>正如我前面提到类型的模型,所以我需要查询我的实体应该是这样的:

public ActionResult Index() 
{ 
    //..something like this..this is IQueryable.. 
    //...convert this to IEnumerable and send this as the model to .. 
    //..the Index View as shown below..here you are querying your own tables, 
    //.. Employee and Job,and binding the result to the EmployeeViewModel which 
    //.. is passed on to the Index view. 
    IEnumerable<EmployeeViewModel> model=null; 
    model = (from c in db.Employees 
       join q in db.Jobs on c.EmployeeId equals q.EmployeeId 
       from q in jobs.DefaultIfEmpty() 
       group new { q, c } by c into grp 
       select new EmployeeViewModel 
       { 
        EmployeeId = grp.Key.EmployeeId, 
        EmployeeName = grp.Key.EmployeeName, 

       }); 

    return View(model); 
} 

我想显示类似的结果:

|Employee 1| 
|----------| 
| Job 1 | 
| Job 2 | 

|Employee 2| 
|----------| 
| Job 3 | 
| Job 4 | 
| Job 5 | 

但现在我的结果是

|Employee 1| |Employee 1| 
|----------| |----------| 
| Job 1 | | Job 2 | 

|Employee 2| |Employee 2| |Employee 2| 
|----------| |----------| |----------| 
| Job 3 | | Job 4 | | Job  | 

如何删除重复项?

回答

0

这里您需要的是在数据库世界中称为LEFT JOIN。在SQL中如何连接两个表有很多种可能性。每个变体都提供了不同的结果。默认JOININNER连接,这意味着结果集中的每个实体都存在于两个表中。 左连连接意味着实体可能存在于两个表中,但是不可以。如果它在第二个表中不存在,则返回一个空的结果集。两个表工作一个例子:

|Table Person | |Table Job  | 
| Id | Name | | Id | Job  | 
|----|---------| |----|-----------| 
| 1 | John | | 1 | Astronaut | 
| 2 | William | | 3 | Cashier | 

默认LINQ连接是INNER加入:

var r = from p in Person 
      join j in Jobs on p.Id equals j.Id 
      select ... 

将提供以下结果集:

| INNER JOIN RESULT | 
| Id | Name | Job  | 
|----|------|-----------| 
| 1 | John | Astronaut | 

The LEFT加入看起来在LINQ是这样的:

var r = from p in Person 
      join j in Jobs on p.Id equals j.Id into jobs 
      from subjob in jobs.DefaultIfEmpty() 
      select ... 
       Job = subjob != null ? subjob.Job : "no job" 
       ... 

而结果现在设置如下:

| LEFT JOIN RESULT  | 
| Id | Name | Job  | 
|----|---------|-----------| 
| 1 | John | Astronaut | 
| 2 | William | no job | 

一个良好的视觉可以解释本文A visual explanation of SQL joins中找到。

通常,LINQ中的默认连接是INNER JOIN,所以只有匹配的实体(在两个表中)才会被选中。你可以做一个LEFT JOIN with LINQ using a second from with DefaultIfEmpty

model = (from e in db.Employees 
      join j in db.Jobs on e.EmployeeId equals j.EmployeeId into jobs 
      from subjob in jobs.DefaultIfEmpty() 
      select new EmployeeViewModel 
      { 
       EmployeeId = e.EmployeeId, 
       EmployeeName = e.EmployeeName, 
       JobName = subjob != null ? subjob.JobName : "no job entry" 
      }).Distinct(); 

微软的良好LINQ的介绍可以在MSDN上找到:101 LINQ samples


集团通过看起来是这样的:

model = (from e in db.Employees 
      join j in db.Jobs on e.EmployeeId equals j.EmployeeId into jobs 
      from subjob in jobs.DefaultIfEmpty() 
      group e by e.EmployeeName into ge 
      select new 
      { 
       Key = ge.Key, 
       Data = ge 
      }); 

foreach (var groupItem in model) 
{ 
    foreach (var employeeViewModel in groupItem.Data) 
    { 
     // ... 
    } 
} 
+0

谢谢你们,它的工作原理。但是当我插入2行与同雇员,看来下面 employee1 JOB1 employee1 作业2 ,我想只有employee1是负载,我怎么能做到这一点,我认为它看起来像不同?请非常感谢你 – user3544501

+0

'employee1 be load'是什么意思?你在你的问题中说'我想显示两个员工'。感谢你们, – pasty

+0

显示都是工作。但是,当我插入2行与EmployeeId相同,它出现在employee1 job1 employee1 job2下面,我想只有employee1被加载,我怎么能做到这一点,我认为它看起来像不同?请非常感谢你 – user3544501

相关问题