我正在尝试使用新的EF Core Shadow属性进行服务器端分页,列表结果的排序和过滤。
排序和分页效果很好,除了我想按实体子属性UserName
对列进行排序时。有没有一种方法可以使用阴影属性按属性子属性进行排序?
像这样的东西:.OrderBy(p => EF.Property<object>(p, "user.UserName"))
实体模型:
public class Player
{
public int Id { get; set; }
public int UserId { get; set; }
public int GamePlayed { get; set; }
public int GameWon { get; set; }
public int Rank { get; set; }
public DateTime LastPlayedDate { get; set; }
public DateTime JoinDate { get; set; }
public bool Active { get; set; }
public virtual User User { get; set; }
}
public class User
{
public int Id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Description { get; set; }
}
查询:
public IEnumerable<Player> GetPlayers(string colName, string filter,
string sortOrder, int pageNumber, int pageSize)
{
var skip = pageNumber * pageSize;
var results = (sortOrder == "asc") ?
_context.Players.Where(p => p.Active)
.OrderBy(p => EF.Property<object>(p, colName))
.Skip(skip).Take(pageSize)
.Include(p => p.User)
:
_context.Players.Where(p => p.Active)
.OrderByDescending(p => EF.Property<object>(p, colName))
.Skip(skip).Take(pageSize)
.Include(p => p.User);
return results;
}