Q:

LINQ to Entities does not recognize the method '<>f__AnonymousType4`1[System.String] get_Item(Int32)'

please I'm working on a ASP.NET MVC project with Entity Framework, I try to use this Query but I got an error .
Query :
var R = (from A in SCHOOL_DB_Context.Con.ABS where A.STG_ABS == STG && (A.DT_ABS.Month + "/" + A.DT_ABS.Year) == MONTHS[i].MONTH && A.DT_ABS.Hour == Hour select A).ToList();

Error :

LINQ to Entities does not recognize the method
'<>f__AnonymousType4`1[System.String] get_Item(Int32)' method, and
this method cannot be translated into a store expression.

The full code is :
    var MONTHS = (from A in SCHOOL_DB_Context.Con.ABS where A.STG_ABS == STG && A.DT_ABS.Hour == Hour group A by A.DT_ABS.Month + "/" + A.DT_ABS.Year into G select new { MONTH = G.Key }).ToList();

    List<DataPoint> DATA = new List<DataPoint>();

    List<DataPoint> DTP = new List<DataPoint>();

    if (MONTHS.Count == 0)
    {

        DTP.Add(new DataPoint(null, null));

    }
    else
    {

        for (int i = 0; i < MONTHS.Count; i++)
        {

            var R = (from A in SCHOOL_DB_Context.Con.ABS where A.STG_ABS == STG && (A.DT_ABS.Month + "/" + A.DT_ABS.Year) == MONTHS[i].MONTH && A.DT_ABS.Hour == Hour select A).ToList();

            int Count = 0;

            Count = R.Count;

            //DATA.Add(new DataPoint(MONTHS[i].MONTH, Count));

            DTP.Add(new DataPoint(MONTHS[i].MONTH, Count));

        }

    }

Please any help to fix this issue?

A:

Try pulling the indexer out of the secondary query:
 var month = MONTHS[i].MONTH;

 var R = (from A in SCHOOL_DB_Context.Con.ABS  
          where A.STG_ABS == STG 
             && (A.DT_ABS.Month + "/" + A.DT_ABS.Year) == month 
             && A.DT_ABS.Hour == Hour
          select A).ToList();

You might still get an error concatenating the month and year as a string - if you do my next suggestion would be to pull the month and year separately in the MONTHS query and compare the two values independently.

