LINQ表达式 g.Inner)' 无法被翻译。请重新编写查询,使其能够被翻译。

6 浏览
0 Comments

LINQ表达式 g.Inner)' 无法被翻译。请重新编写查询,使其能够被翻译。

帮我修复或解决这个问题

我的任务:修正猜对次数/游戏次数。如果成功率相同,则总尝试次数较少的玩家排名较高。输入最小游戏次数N - 只有玩了至少N次游戏的玩家才会被纳入排行榜。

我的玩家模型:

[Table("players")]
public class Player
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string Id { get; set; }
    [Required]
    [Display(Name = "Player name")]
    public string Name { get; set; }
    [JsonIgnore]
    public List Games { get; set; }
}

我从游戏表中创建排行榜列表,这是我的游戏模型:

[Table("games")]
public class Game
{
    public Game()
    {
        this.StartDate = DateTime.Now;
        this.Attempts = 0;
        this.EndDate = null;
        this.Result = GameResultType.InGame;
    }
    [Key]
    public int ID { get; set; }
    [Required]
    [Display(Name = "Player")]
    public Player Player
    {
        get;
        set;
    }
    [Display(Name = "Count Attempts")]
    public int Attempts
    {
        get;
        set;
    }
    [Required]
    [Display(Name = "Random number")]
    [JsonIgnore]
    public int RandomNumber
    {
        get;
        set;
    }
    [Display(Name = "Game start datetime")]
    public DateTime StartDate
    {
        get;
        set;
    }
    [Display(Name = "Game end datetime")]
    public DateTime? EndDate
    {
        get;
        set;
    }
    [Display(Name = "Status of the game")]
    public StatusType Status
    {
        get;
        set;
    }
    [Display(Name = "Status of the game")]
    public GameResultType Result
    {
        get;
        set;
    }
}

我应该得到至少玩了N次游戏的玩家排行榜

这是我的查询语句:

var noticesGrouped = _context.Games
                .GroupBy(n => n.Player)
                .Select(group =>
                     new
                     {
                         PlayerName = group.Key.Name,
                         Games = group.Key.Games.ToList(),
                         Count = group.Key.Games.Count()
                     }).Where(x => x.Games.Count > 1).ToList();

0
0 Comments

LINQ(Language Integrated Query)是一种用于查询和操作数据的统一编程模型。在使用LINQ时,有时会遇到"The LINQ expression 'g.Inner' could not be translated. Either rewrite the query in a form that can be translated"的错误。这篇文章将探讨这个问题出现的原因以及解决方法。

首先,需要了解LINQ to SQL和LINQ to Objects之间的区别。LINQ to SQL是用于与数据库进行交互的LINQ提供程序,它将LINQ查询转换为SQL查询,从而实现与数据库的交互。而LINQ to Objects是用于在内存中操作对象集合的LINQ提供程序,它将LINQ查询转换为.NET对象操作。

当使用LINQ查询时,如果查询涉及到多个数据源,例如同时涉及数据库和内存中的对象集合,就会出现上述错误。原因是LINQ to SQL无法将LINQ查询转换为SQL查询,因为它无法识别内存中的对象集合。

为了解决这个问题,可以使用"materialize"操作将对象转换为内存中的对象集合,从而使得LINQ to Objects可以处理查询。在上述代码示例中,通过在查询链中添加`.AsEnumerable()`,可以将查询结果转换为内存中的对象集合。然后,可以继续使用LINQ to Objects操作进行进一步的查询。

通过这种方式,可以解决"The LINQ expression 'g.Inner' could not be translated. Either rewrite the query in a form that can be translated"错误。然后,可以继续使用LINQ to Objects进行查询和操作。

希望通过阅读本文,你对"The LINQ expression 'g.Inner' could not be translated. Either rewrite the query in a form that can be translated"错误的原因和解决方法有了更好的理解。如果你想了解更多相关信息,可以参考下面的链接:

- [Are Linq to SQL and Linq to Objects queries the same?](https://stackoverflow.com/questions/1052508)

- [The LINQ expression could not be translated and will be evaluated locally](https://stackoverflow.com/questions/57872910)

0