MVC 4. and Entity Framework Table Join

asp.net-mvc asp.net-mvc-4 c# entity-framework linq

Question

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

1
6
3/14/2013 6:04:46 PM

Accepted Answer

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,
  {
}
6
3/14/2013 7:52:32 PM

Popular Answer

   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();



    }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow