2017-10-16 42 views
0

我有以下linq查詢可以正常工作,但我想從context.Emps中將一列(CompanyId)與來自上下文的結果一起拖放到結果中.BillingProfiles。我如何修改下面的select(select prof)以包含所述列?如何從LINQ查詢中的第二個表中拉出一列加入

var query = (from prof in context.BillingProfiles 
      join emp in context.Emps on prof.ID equals emp.ID 
      join grp in context.BillingGroups on prof.GroupID equals grp.GroupID 
      where (prof.EndDate == null) && (grp.System == "sysGrp") && (prof.ID == id) 
      select prof).Distinct() 
      .Select(x => new OpId() 
      { 
       id = x.ID,           
       GroupId = x.GroupID, 
       OpId = x.OpID, 
       StartDate = x.StartDate, 
       EndDate = x.EndDate, 
       AddedOn = x.AddedOn, 
       AddedBy = x.AddedBy, 
       RemovedOn = x.RemovedOn, 
       RemovedBy = x.RemovedBy, 
       Prodid = x.ProdID, 
      }); 

感謝

回答

1

項目包含那些過於匿名對象:

var query = from prof in context.BillingProfiles 
      join emp in context.Emps on prof.ID equals emp.ID 
      join grp in context.BillingGroups on prof.GroupID equals grp.GroupID 
      where prof.EndDate == null && prof.ID == id && grp.System == "sysGrp" 
      select new { prof, emp.CompanyId, grp }; 
+1

要多一點背景添加到吉拉德的答案,你會寫你的最終選擇後的匿名對象是這樣的: ()); select new {prof,emp.CompanyID,grp})。Distinct()。Select(x => new OpId(){//使用prof,grp等 }); – Peter

相關問題