Entity-framework – LINQ to Entities does not recognize the method … get_Item(Int32)

entity-frameworkentity-framework-4

(Please read before marking as duplicate as my particular scenario is unique)

I have the following code:

        // Get each treasure hunt
        var treasureHunts = dbContext.TreasureHunts.Where(i => i.UserName == User.Identity.Name).ToList();

        // Populate each treasure hunt with the list of leaderboard entries
        for (int i = 0; i <= treasureHunts.Count; i++)
        {
            treasureHunts[i].Leaderboard = dbContext.Leaderboard.Where(
                leaderboard => leaderboard.TreasureHuntId == treasureHunts[i].TreasureHuntId).ToList();
        }

On running the program, I get the following error from the second database query (dbContext.Leaderboard.Where…):

LINQ to Entities does not recognize the method
'QrCodeTreasureHunter.Models.TreasureHuntDetails get_Item(Int32)'
method, and this method cannot be translated into a store expression.

In the first query, I'm getting each of the treasure hunts associated with a particular user.

In the second part, I'm attempting to iterate through each of the treasure hunts, and populate the treasure hunt's Leaderboard List property with the associated leaderboard entries from my Leaderboard table.

From what I understand from reading around is that this query isn't possible in its current form with Entity Framework.

What workarounds or solutions would you be able to recommend to solve this problem? The ideal solution would involve no changes to the data models.

If it's relevant, here is the TreasureHunt model:

    public class TreasureHuntDetails
{
    public TreasureHuntDetails()
    {
        Clues = new List<Clue>();
        Leaderboard = new List<Leaderboard>();

        var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
        var dcs = new DataContractSerializer(typeof (TreasureHuntDetails), null, int.MaxValue,
                                             false, true, null);
        xml.SetSerializer<TreasureHuntDetails>(dcs);
    }

    [Key]
    public int TreasureHuntId { get; set; }
    [Required]
    public virtual string UserName { get; set; }
    [Required]
    public String Name { get; set; }
    public String Description { get; set; }
    [Required]
    public String Password { get; set; }
    public String CompletionMessage { get; set; }
    public String State { get; set; }
    public List<Clue> Clues { get; set; }
    public List<Leaderboard> Leaderboard { get; set; } 
}

And here is the Leaderboard model:

    public class Leaderboard
{
    [Key]
    public int Id { get; set; }
    public int TreasureHuntId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public int Completion { get; set; }

    public DateTime? StartTime { get; set; }
    public DateTime? EndTime { get; set; }
    public Int64 TimeTaken { get; set; }

    public TreasureHuntDetails TreasureHuntDetails { get; set; }
}

Good luck!

Best Answer

I'm not able to test it right now but it could be the indexer, try this:

foreach (var treauserHunt in treasureHunts)
{
    treasureHunt.Leaderboard = dbContext.Leaderboard.Where(leaderboard => 
        leaderboard.TreasureHuntId == treasureHunt.TreasureHuntId).ToList();
}

I'm not sure this is the problem, but I remember having some issues with indexing in arrays in LINQ queries, just can't remember if it was with the LINQ method syntax (the one you are using) or the other (the SQL-like);

Related Topic