Mad*_*ann 2 entity-framework row count
希望有人可以帮助我,因为我有点卡住了.
我正在一个游戏的核心数据库前面建立一个服务.
该数据库有以下两个表:
CREATE TABLE [dbo].[PB_HiscoreEntry] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[PlayerId] UNIQUEIDENTIFIER NOT NULL,
[Score] INT NOT NULL,
[DateCreated] DATETIME NOT NULL
);
CREATE TABLE [dbo].[PB_Player] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[UniquePlayerId] NCHAR (32) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[DateCreated] DATETIME NOT NULL
);
Run Code Online (Sandbox Code Playgroud)
这个想法当然只是让每个玩家一次进入数据库并让他们拥有多个组织参赛作品.这个表PB_HiscoreEntry会有很多分数,但是通过简单的OrderBy降序,我可以创建一个真正的组织列表,其中得分最高的那个位于顶部,最低位于底部.我的问题是我的数据库与其他数据库相比,不知道得分的实际等级.这是我在执行上述OrderBy查询时应该做的事情.
这里有一些代码可以帮助我证明我想要存档的内容:
var q = (
from he in entities.PB_HiscoreEntry
orderby he.Score descending
select new HiscoreItem()
{
UserId = he.PB_Player.UniquePlayerId,
Username = he.PB_Player.Name,
Score = he.Score,
//Put in the rank, relative to the other entires here
Rank = 1
});
Run Code Online (Sandbox Code Playgroud)
HiscoreItem,只是我自己需要通过电线发送的DTO.
所以任何人都知道如何做到这一点,还是我在这里走错了路?
你走在正确的轨道上,你只需要使用Queryable.Select带有额外索引的重载.看看这个:
var entries =
from entry in entities.PB_HiscoreEntry
orderby entry.Score descending
select entry;
// Note the (entry, index) lambda here.
var hiscores = entries.Select((entry, index) => new HiscoreItem()
{
UserId = entry.PB_Player.UniquePlayerId,
Username = entry.PB_Player.Name,
Score = entry.Score,
Rank = index + 1
});
Run Code Online (Sandbox Code Playgroud)
我不是100%确定实体框架是否知道如何处理
Select<TSource, TResult>(this IQueryable<TSource>, Expression<Func<TSource, int, TResult>>)重载.如果是这种情况,只需使用静态Enumerable类的等效方法:
// Note the .AsEnumerable() here.
var hiscores = entries.AsEnumerable()
.Select((entry, index) => new HiscoreItem()
{
UserId = entry.PB_Player.UniquePlayerId,
Username = entry.PB_Player.Name,
Score = entry.Score,
Rank = index + 1
});
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助.
| 归档时间: |
|
| 查看次数: |
9942 次 |
| 最近记录: |