This item is driving me mad ;-) I am trying to do 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 keep getting an error:
{"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 so hard to do a simple join between two tables and populate the variable in Entity framework
Thanks
Project to an 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 map back to your entity
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,
  {
}
                           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();
    }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With