LINQ to Entities does not recognize the method … get_Item(Int32)

*爱你&永不变心* 提交于 2019-12-24 14:36:43

问题


(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!


回答1:


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);




回答2:


Thanks for the answer; it solved my problem. I still wanted to use a for loop so I did something like this:

for (int i = 0; i <= treasureHunts.Count; i++)
    {
        var thisTreasureHunt = treasureHunts[i];

        treasureHunts[i].Leaderboard = dbContext.Leaderboard.Where(
            leaderboard => leaderboard.TreasureHuntId == thisTreasureHunt.TreasureHuntId).ToList);
    }


来源:https://stackoverflow.com/questions/15695825/linq-to-entities-does-not-recognize-the-method-get-itemint32

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!