locked
System.NotSupportedException in join query RRS feed

  • Question

  • Hello,

    I'm trying to do the following query:

    public IQueryable<Coctact> GetCoctactsByCompeteNameAndCase(string name, string lastName, int caseNumber)
            {
                  var query = from cases in ObjectContext.Cases 
                            from contact in ObjectContext.Coctacts
                            where 
                            cases.ContactID.Equals(contact.ContactID) && 
                             cases.CaseID.Equals(caseNumber)
                                 select contact;    
                
                return query;
            }

    but I always recive this exception:

    System.NotSupportedException was unhandled by user code
      Message=Unable to create a constant value of type 'System.Object'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
      Source=System.Data.Entity
      StackTrace:
           at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)
           at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)

    I also have tried this way:

    public IQueryable<Coctact> GetCoctactsByCompeteNameAndCase(string name, string lastName, int caseNumber)
            {
                
                var query = from cases in ObjectContext.Cases 
                            join contact in ObjectContext.Coctacts
                            on cases.ContactID.Equals(contact.ContactID) 
                            where
                                cases.CaseID.Equals(caseNumber)
                            select contact;
    
                List<Coctact> list = query.ToList();
                
                return query;
            }

    But it don't compile and I dont know Why?

    Can you please help me!

    thanks.

     

     

     


    paburgos
    Saturday, April 10, 2010 5:06 PM

Answers

  • Wait, do you even need to do the join? Does a Case object have a navigation property of Contact(or as you have Coctact that's a mispelling, I think)? I would think so because I see Case.ContactID.

    Can you post an image (or link to an image) of your model so we can take a look at how the model is configured?

    Because I would think you don't need the join and could just do

    List<Coctact> list = (from cases in ObjectContext.Cases 
                            where cases.CaseID == caseNumber &&
    			cases.Contact.LastName == lastName
                            select contact).ToList();

     

    • Marked as answer by paburgos Sunday, April 11, 2010 5:52 AM
    Sunday, April 11, 2010 3:43 AM
  • Hi paburgos,

     

    I agree with paully21.   We may use the navigation properties to make the query easier.   You can provide us with more detailed information about the EDM.  

     

    Besides, for the second join query in your post, it can be such a query:

    =======================================================================================

    var query = from cases in ObjectContext.Cases

                       where cases.CaseID == caseNumber

                       join contact in ObjectContext.Contacts

                       on cases.ContactID equals contact.ContactID

                       select contact;

    =======================================================================================

     

    If you have any questions, please feel free to let us know.

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by paburgos Sunday, April 11, 2010 5:52 AM
    Sunday, April 11, 2010 4:55 AM

All replies

  • Wait, do you even need to do the join? Does a Case object have a navigation property of Contact(or as you have Coctact that's a mispelling, I think)? I would think so because I see Case.ContactID.

    Can you post an image (or link to an image) of your model so we can take a look at how the model is configured?

    Because I would think you don't need the join and could just do

    List<Coctact> list = (from cases in ObjectContext.Cases 
                            where cases.CaseID == caseNumber &&
    			cases.Contact.LastName == lastName
                            select contact).ToList();

     

    • Marked as answer by paburgos Sunday, April 11, 2010 5:52 AM
    Sunday, April 11, 2010 3:43 AM
  • Hi paburgos,

     

    I agree with paully21.   We may use the navigation properties to make the query easier.   You can provide us with more detailed information about the EDM.  

     

    Besides, for the second join query in your post, it can be such a query:

    =======================================================================================

    var query = from cases in ObjectContext.Cases

                       where cases.CaseID == caseNumber

                       join contact in ObjectContext.Contacts

                       on cases.ContactID equals contact.ContactID

                       select contact;

    =======================================================================================

     

    If you have any questions, please feel free to let us know.

     

    Have a nice weekend!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by paburgos Sunday, April 11, 2010 5:52 AM
    Sunday, April 11, 2010 4:55 AM
  • Thank you very much paully21 and Lingzhi Sun,


    All the queries that you provided me worked! I did have a misspelling, it was Contact. Regarding the EDM I forgot to attach it but you guys inferred it correctly. I'm a beginner with EF and Linq, I guess I'm still in T-SQL mode but I very excited with the whole EF concept.


    Again thank you very much for helping me!

     


    paburgos
    Sunday, April 11, 2010 5:52 AM
  • You are welcome!  J

     

    If you encounter any problems in EF, feel free to post here and we can have some discussion.

     

    Have a nice day!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, April 12, 2010 5:50 AM