MVC 4. and Entity Framework Table Join

This subject is driving me crazy ;-) I'm trying to make a simple query joining two tables

I have the following:

Repository Class Method

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

In my controller:

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

I get an error message:

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

I also tried adding to the repository class:

Repository Class Method

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

Is it really that difficult to make a simple join between two tables and populate a variable in the Entity infrastructure

thanks

+6
source share
2 answers

Project for anonymous object

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

Then go back to your essence

 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, { } 
+6
source
  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(); } 
+2
source

All Articles