none
LINQ expression thorws exception "LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' RRS feed

  • Question

  • Hi I have entity ModesMaster has following two properties:
    1. Name - string
    2. ID - int

    And following two LINQ expressions:

    1. int enabledModeID = (from Mode in ModesMaster
                          where Mode.Name.Equals(ConstantsHelper.SUMMARY_MODE, StringComparison.OrdinalIgnoreCase)
                          select Mode.ID).First<int>();

    2. int enabledModeID = (from Mode in ModesMaster
                         where Mode.Name.Equals("Detailed", StringComparison.OrdinalIgnoreCase)
                         select Mode.ID).First<int>();

    The first ones works fine without any exception while the second one throws following exception:

    {"LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression."}

    Kinldy, let me know why is this so?

    Both the statements are exactly the same except for the filter criteria.

    Regards,

    Sachin R. Chavan.


    Sachin R. Chavan.

    Wednesday, September 17, 2014 6:25 AM

All replies

  • Hello Sachin,

    What item type are you working with, Entity Framework 5/6?

    I made a similar query with yours by using Entity Framework:

    using (DFDBEntities db = new DFDBEntities())
    
                {
    
                    var result = (from o in db.Orders
    
                                  where o.OrderName.Equals("detail", StringComparison.OrdinalIgnoreCase)
    
                                  select o.OrderID).First<int>();
    
                }
    

    It could work as expected.

    And for this error “{"LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression."}”, it shows the reason directly, method as ToString(), get_Item() cannot be translated to sql statement so that LINQ2Entities does not support them. An alternate way is to use LINQ2Object, you could try with as below, adding the ToList()

    If this does not work for you, could you please share the ModesMaster object with us as I notice this is only difference between mine and yours?

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, September 17, 2014 10:01 AM
    Moderator
  • Actually, ModesMaster is a DB entity and since its created for client project, I had re-modeled it so that it hides the actual model and only represents the required stuff.

    I am using EF 5. You converted the result to var instead of int.


    Sachin R. Chavan.

    Wednesday, September 17, 2014 2:15 PM
  • Hello Sachin,

    Would you mind sharing your model with us?

    >>You converted the result tovar instead of int.

    The var is implicitly typed, in my case, it actually equals with "int result= ...".


    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 18, 2014 7:32 AM
    Moderator
  • I also re-write the above code as follows:

    ModesMaster enabledMode = (from Mode in ModesMaster

    where Mode.Name.Equals(enabledModes[0], StringComparison.OrdinalIgnoreCase)

                                select Mode).First<ModesMaster>();

    int enabledModeID = enabledMode.ID;

    But still getting the same exception:

    {"LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression."}

    I dont think we should get above exception now. Should I?


    Sachin R. Chavan.

    Thursday, September 18, 2014 7:41 AM
  • Hi,

    >> I dont think we should get above exception now. Should I?

    I am not sure since I do not know your exact model, to narrow down this issue, I would suggest that you could write code as my original post and replace the Order Entity with the ModesMaster without the middle layer to check if this is caused by re-modeled entity.

    And if it is possible, you could upload your project to skydrive so that we could download it and make a test with it.

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, September 18, 2014 9:09 AM
    Moderator