2017-02-01 351 views
0

我想加入我的兩個表linq基於一個id,迄今不正常。如何使用linq連接兩個表?

這裏是我的模型的樣子:

public class WorkRole 
    { 

     public int WorkRoleId { get; set; } 
     public string RoleName { get; set; } 
     public string RoleDescription { get; set; } 
     public int CompanyId { get; set; } 
     public virtual Company Company { get; set; } 
     public virtual ICollection<WorkRolesUsersDetails> WorkRolesUsersDetails { get; set; } 
    } 



public class WorkRolesUsersDetails 
    { 

     public int WRUDId { get; set; } 

     public int? WorkRoleId { get; set; } 


     public string UserDetailsId { get; set; } 


     public virtual WorkRole WorkRole { get; set; } 

     public virtual UserDetails UserDetails { get; set; } 

     public DateTime FocusStart { get; set; } 
     public DateTime FocusEnd { get; set; } 

     public bool isActive { get; set; } 
    } 

我想在一個視圖中WorkRoleId,角色名,RoleDescription和CompanyId從第一個表和第二個表UserDetailsId,FocusStart,FocusEnd和isActive得到。

我有我的想法得到了最遠的是:

var query = db.WorkRoles.Join(db.WorkRolesUsersDetails,x => x.WorkRoleId,y => y.WorkRoleId,(x, y) => new { wr = x, wrud = y }); 

但可悲的是,它沒有工作。我只是不知道足夠多的linq,並且無法從這裏獲得其他問題/答案。請幫忙。

+0

您did'nt意味着什麼工作,任何異常或預期的結果did'nt來的? –

回答

2

代碼加入2個表是:

var list = db.WorkRoles. 
       Join(db.WorkRolesUsersDetails, 
       o => o.WorkRoleId, od => od.WorkRoleId, 
       (o, od) => new 
       { 
        WorkRoleId= o.WorkRoleId 
        RoleName= o.RoleName, 
        RoleDescription= o.RoleDescription, 
        CompanyId= o.CompanyId, 
        WRUDId= od.WRUDId, 
        UserDetailsId= od.UserDetailsId, 
        FocusStart=od.FocusStart, 
        FocusEnd=od.FocusEnd 
       }) 
1

如果您使用EF,我可能會建議使用Includes語句,它的工作原理是奇蹟。如果你有一個外鍵分配。它基本上獲得了其他數據。

static void Main(string[] args) 
{ 
    using (var context = new TesterEntities()) 
    { 
    var peopleOrders = context.tePerson.Include("teOrder").First(p => p.PersonId == 1).teOrder.ToList(); 
    peopleOrders.ForEach(x => Console.WriteLine($"{x.OrderId} {x.Description}")); 
    } 
} 

手動組合手動導航上下文選項。

public class Student 
    { 
    public int StudentID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 

    public List<StudentTestScore> Scores { get; set; } 
    } 

    public class StudentTestScore 
    { 
    public int StudentID { get; set; } 
    public int Score { get; set; } 
    } 

    class Program 
    { 

    static void Main(string[] args) 
    { 
     var students = new List<Student> 
     { 
     new Student { StudentID = 1, FirstName = "Brett", LastName = "X" }, 
     new Student { StudentID = 2, FirstName = "John", LastName = "X" } 
     }; 
     var grades = new List<StudentTestScore> { new StudentTestScore { StudentID = 1, Score = 98 } }; 

     var combined = students.Join(grades, x => x.StudentID, y => y.StudentID, 
       (x, y) => new 
       { 
        Student = $"{x.FirstName} {x.LastName}", 
        Grade = y.Score 
       }).ToList(); 

     combined.ForEach(x => Console.WriteLine($"{x.Student} {x.Grade}")); 

     Console.ReadLine(); 
    } 
+0

請看看我的答案。我也這麼認爲,但不確定它是EF還是LINQ問題。 – Michael

+0

您沒有從您的示例中看不到您的連接對象的屬性。你不會做'new {thing = x,thing2 = y}'。你要做'新的東西= x.PROPERTY,thing2 = y.PROPERTY}'。查看更新的示例。 – djangojazz