none
Is it C# or Entity Framework Limitation? RRS feed

  • Question

  • The following code fails:

    InstanceOfIObjectSet.SingleOrDefault(r => r.ID == myObj.Collection[i].PublicMemberVar);
    

    If I was to extract PublicMemberVar into its own variable, the code will work without exception:

    int myValue = myObj.Collection[i].PublicMemberVar;
    InstanceOfIObjectSet.SingleOrDefault(r => r.ID == myValue);
    
    

    Where does the limitation come from? Is it C# or is it part of entity framework?

    Why is there a limitation?

     

    Thanks

     

    • Moved by Paul Zhou Monday, January 23, 2012 5:29 AM move for better support (From:Common Language Runtime)
    Friday, January 20, 2012 7:51 PM

Answers

  • C# is a coding conversion. It does not throw exception on its own, as its just a bunch of rules people agreed on. To throw exception you need to have the code first, then ask the compiler to translate to MSIL, then CLR JIT the IL to native code. Those are just a bunch of translators to convert your code from one form to another, they do not run your code, less throw exceptions on your behalf.

    The exception could be thrown anywhere from the database engine's Entity Framework provider or the entity framework itself, or if you switched from Linq to Entities to Linq to Objects midway as your are not returning IQueryable <T> somewhere but instead returned IEnumerable, from Net's BCL's linq to object provider. The call stack should be able to tell where the exception is thrown. 

     



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Friday, January 20, 2012 11:45 PM

All replies

  • C# is a coding conversion. It does not throw exception on its own, as its just a bunch of rules people agreed on. To throw exception you need to have the code first, then ask the compiler to translate to MSIL, then CLR JIT the IL to native code. Those are just a bunch of translators to convert your code from one form to another, they do not run your code, less throw exceptions on your behalf.

    The exception could be thrown anywhere from the database engine's Entity Framework provider or the entity framework itself, or if you switched from Linq to Entities to Linq to Objects midway as your are not returning IQueryable <T> somewhere but instead returned IEnumerable, from Net's BCL's linq to object provider. The call stack should be able to tell where the exception is thrown. 

     



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Friday, January 20, 2012 11:45 PM
  • Hi,

    I agree with Sheng Jiang!  The problem may be caused by EF data provider cannot transfer the object myObj.Collection[i].PublicMemberVar into certain SQL command.  However, if we pass a local variable myValue, the provider could understand it and directly convert it into integer. 

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 23, 2012 6:16 AM
    Moderator
  • Hi,

    And as a side note it's always better to state the exact exception you have...

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Monday, January 23, 2012 12:14 PM