2013-03-14 67 views
6

此產品是推動我瘋了;-) 我試圖做一個簡單的查詢,將兩個表MVC 4和Entity Framework表加入

我有以下幾點:

倉儲類方法

public IQueryable<ADPerson> FindAll(string UserId) 
    { 
     return (from p in db.ADPerson 
       select p); 


    } 

在我的控制器:

var ADPersonList = from o in ADPersonDB.FindAll(GetUserId()) 
        join c in MSDNTypeDB.FindAll(GetUserId()) on o.MsdnTypeId equals c.MsdnTypeId 
        select new ADPerson() 
        { 

         AdPersonId = o.AdPersonId, 
         SamAccountName = o.SamAccountName, 
         Description = o.Description, 
         DisplayName = o.DisplayName, 
         UserPrincipalName = o.UserPrincipalName, 
         Enabled = o.Enabled, 
         LastUpdated = o.LastUpdated, 
         OnlineAssetTag = o.OnlineAssetTag, 
         MsdnTypeId = o.MsdnTypeId, 
         MsdnSubscription = c.MsdnTypeDescription, 


        }; 

我不斷收到一封RROR:

{"The specified LINQ expression contains references to queries that are associated with different contexts."} 

我也嘗試添加到存儲庫類:

倉儲類方法

public IQueryable<ADPerson> FindAll(string UserId) 
    { 
     //return (from p in db.ADPerson 
     //  select p); 

     var query = from o in db.ADPerson 
        join c in db.MsdnTypes on o.MsdnTypeId equals c.MsdnTypeId 
        select new ADPerson() 
        { 

         AdPersonId = o.AdPersonId, 
         SamAccountName = o.SamAccountName, 
         Description = o.Description, 
         DisplayName = o.DisplayName, 
         UserPrincipalName = o.UserPrincipalName, 
         Enabled = o.Enabled, 
         LastUpdated = o.LastUpdated, 
         OnlineAssetTag = o.OnlineAssetTag, 
         MsdnTypeId = o.MsdnTypeId, 

         MsdnSubscription = c.MsdnTypeDescription, 


        }; 

     return query; 

    } 

真的就那麼難做到兩個表之間的簡單連接,並填充變量實體框架

謝謝

+0

是否使用第二種方法同樣的錯誤? – mattytommo 2013-03-14 18:04:20

+0

優秀點:否第二個返回的錯誤是:實體或複雜類型'project.Models.ADPerson'不能在LINQ to Entities查詢中構造。 – 2013-03-14 18:06:22

+0

RE:第二個錯誤。那是因爲你不能*投影到映射的實體上。您可以將查詢投影到匿名對象,然後將其映射到ADPerson實體 – 2013-03-14 18:09:27

回答

6

項目到一個匿名對象

var query = from o in db.ADPerson 
    join c in db.MsdnTypes on o.MsdnTypeId equals c.MsdnTypeId 
    select new 
    { 
    AdPersonId  = o.AdPersonId, 
    SamAccountName = o.SamAccountName, 
    Description  = o.Description, 
    DisplayName  = o.DisplayName, 
    UserPrincipalName = o.UserPrincipalName, 
    Enabled   = o.Enabled, 
    LastUpdated  = o.LastUpdated, 
    OnlineAssetTag = o.OnlineAssetTag, 
    MsdnTypeId  = o.MsdnTypeId, 
    MsdnSubscription = c.MsdnTypeDescription, 
    }; 

然後映射回你的實體

foreach (var item in query) 
{ 
    var adPerson = new ADPerson 
    { 
    AdPersonId   = item.AdPersonId, 
    SamAccountName  = item.SamAccountName, 
    Description  = item.Description, 
    DisplayName  = item.DisplayName, 
    UserPrincipalName = item.UserPrincipalName, 
    Enabled   = item.Enabled, 
    LastUpdated  = item.LastUpdated, 
    OnlineAssetTag  = item.OnlineAssetTag, 
    MsdnTypeId   = item.MsdnTypeId, 
    MsdnSubscription = item.MsdnTypeDescription, 
    { 
} 
+0

第一個var查詢失敗是否有假設選擇新? – 2013-03-14 19:50:46

+0

@DavidCostelloe是的,對不起。編輯回答 – 2013-03-14 19:52:50

+0

獲取錯誤「無效的列名'MsdnSubscription」字段 – 2013-03-14 20:50:18

2
public IQueryable<ADPerson> FindAll(string UserId) 
    { 
     // return (from p in db.ADPerson 
     //  select p); 
     List<ADPerson> lst = new List<ADPerson>(); 
     var query = from o in db.ADPerson 
        join c in db.MsdnTypes on o.MsdnTypeId equals c.MsdnTypeId 
        select new 
         { 

          AdPersonId = o.AdPersonId, 
          SamAccountName = o.SamAccountName, 
          Description = o.Description, 
          DisplayName = o.DisplayName, 
          UserPrincipalName = o.UserPrincipalName, 
          Enabled = o.Enabled, 
          LastUpdated = o.LastUpdated, 
          OnlineAssetTag = o.OnlineAssetTag, 
          MsdnTypeId = o.MsdnTypeId, 
          MsdnSubscription = c.MsdnTypeDescription 

         }; 
     foreach (var item in query) 
     { 
      var adPerson = new ADPerson() 
       { 
        AdPersonId = item.AdPersonId, 
        SamAccountName = item.SamAccountName, 
        Description = item.Description, 
        DisplayName = item.DisplayName, 
        UserPrincipalName = item.UserPrincipalName, 
        Enabled = item.Enabled, 
        LastUpdated = item.LastUpdated, 
        OnlineAssetTag = item.OnlineAssetTag, 
        MsdnTypeId = item.MsdnTypeId, 
        MsdnSubscription = item.MsdnSubscription 
       }; 
      lst.Add(adPerson); 

     } 

     return lst.AsQueryable(); 



    } 
+0

它可能是你有一個尾隨逗號在「MsdnSubscription = c.MsdnTypeDescription」,也在第二個 – eburgos 2013-03-14 20:11:07

+0

謝謝發現它是支柱{和};幾乎在那裏:-) – 2013-03-14 20:32:25

相關問題