6

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

5
  • Is it the same error using the second method? Commented Mar 14, 2013 at 18:04
  • Excellent point: No the error returned on the second is: The entity or complex type 'project.Models.ADPerson' cannot be constructed in a LINQ to Entities query. Commented Mar 14, 2013 at 18:06
  • RE: the second error. That's because you cannot project onto a mapped entity. You may project the query to an anonymous object, and then map it to the ADPerson entity afterwards Commented Mar 14, 2013 at 18:09
  • Alternatively, you may choose to create an un-mapped DTO, project to that, then map back to the ADPerson entity. Commented Mar 14, 2013 at 18:10
  • Oh so return as List rather than IQuerable? Sorry little confused as both tables are mapped in repository classes..ok not sure how to create an un-mapped DTO and map back again Commented Mar 14, 2013 at 18:15

2 Answers 2

6

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,
  {
}
Sign up to request clarification or add additional context in comments.

7 Comments

The first var query is failing is there suppose to be select new?
@DavidCostelloe yes, sorry. Edited answer
getting error "Invalid column name 'MsdnSubscription" on display of field
I should be returning query but it says I can't due to being anonymous.
Looks like I need to convert the anonymous to Iquerable<ADPerson> any ideas on how i can do that. Does seem like an aweful lot of trouble to join a table in the Entity framework there must be an easier way?
|
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();



    }

2 Comments

it might be you have a trailing comma at "MsdnSubscription = c.MsdnTypeDescription," and also on the second one
Thanks found it was the brace { and }; almost there :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.