C#Linq-to-Entities OrderByDescending无法正常工作

时间:2018-12-30 17:30:59

标签: c# sql-server linq entity-framework-6

这是我的实体,名为SportTeam

public class SportTeam : BaseEntity
{
    public int Id { get; set; } 
    public int SportId { get; set; }
    public int TeamId { get; set; }

    public int TotalPlays { get; set; }
    public int TotalWins { get; set; }
    public int TotalDefeats { get; set; }
    public int TotalDraws { get; set; }
    public float WinPercentage { get; set; }
    public float Score { get; set; }

    // navs
    public virtual Ladder Ladder { get; set; }
    public int? LadderId { get; set; }
    public virtual Sport Sport { get; set; }
    public virtual Team Team { get; set; }
    public ICollection<Match> HomeMatches { get; set; }
    public ICollection<Match> RivalMatches { get; set; }
    public ICollection<Match> VictorMatches { get; set; }
    public virtual ICollection<TeamMember> TeamMembers { get; set; }
    public virtual ICollection<Tournament> Tournaments { get; set; }

    public override bool Equals(object obj)
    {
        SportTeam sportTeam = (SportTeam)obj;

        if (sportTeam.Id == Id)
            return true;
        else 
            return false;
    }

    public override int GetHashCode()
    {
        return 2108858624 + Id.GetHashCode();
    }
}

我正在尝试通过得分对SportTeam实体进行评分

List<SportTeam> sportTeams =
                dbContext.SportTeams
                         .Where(st => st.SportId == sportTeam.SportId)
                         .OrderByDescending(st => sportTeam.Score)
                         .ToList();

但是,当我在sportTeams列表上添加手表时,该列表似乎根本没有按分数排序。我只是得到表的表示形式。

有任何提示吗?

1 个答案:

答案 0 :(得分:8)

您正在按恒定值(sportTeam.Score)进行排序

OrderByDescending(st => st.Score)

代替

OrderByDescending(st => sportTeam.Score)