C# 在实体框架中的外部列表中使用.skip()的最佳方法

C# 在实体框架中的外部列表中使用.skip()的最佳方法,c#,entity-framework,C#,Entity Framework,我有两张桌子: public class ApplicationUser : IdentityUser { public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) { var userIdentity = await manager.CreateIdentityAsync(this, DefaultAu

我有两张桌子:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }

    [ForeignKey("ApplicationUserId")]
    public virtual ICollection<ApplicationUserNotification> ApplicationUserNotifications { get; set; }
}

public class ApplicationUserNotification
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }

    [StringLength(64)]
    public string ApplicationUserId { get; set; }
    [ForeignKey("ApplicationUserId")]
    public virtual ApplicationUser ApplicationUser { get; set; }
}
但他说:

“ICollection”不包含“Skip”的定义,最佳扩展方法重载“Queryable.skipqueryable,int”需要类型为“IQueryable”的接收器

在不改变代码直接从usernotification表查询的情况下,解决这个问题的最佳方法是什么?这段代码的实际使用使用了user类,因此如果我可以通过user对象访问usernotifications,那就更好了

这很奇怪,我本以为会有很多关于这个的问题,但是搜索引擎似乎跳过了skip这个词,所以我找不到关于这个的任何东西

=====================

更新mick,我在视图中使用它,我有一个静态对象,可以访问名为Current的用户对象:

@{
    Layout = null;
    var skip = Convert.ToInt32(Request["Skip"] ?? "0");
    var take = Convert.ToInt32(Request["Take"] ?? "10");
}

@foreach (var item in Current.User.UserNotifications.Skip(skip).Take(take))
{
    @Html.Partial(Enum.GetName(typeof(Kiteshoot.Enums.NotificationType), item.NotificationType), item)
}

<div class="UserNotificationsEndOfList" data-index="@(skip + take)"></div>

我真的不知道为什么,但错误已经神奇地消失了,我已经编码太久了,我想,对不起。但正如Stijn所说,这不会跳过查询,而只会跳过内存中的列表,所以回到第一步。

如果要使用skip和take仅从数据库加载部分通知,请使用

DbSet<ApplicationUserNotification>()
    .Where(n => n.ApplicationUserId == User.Identity.GetUserId())
    .Skip(() => skip)
    .Take(() => take)
    .ToList()

如果您想知道为什么.Skip=>Skip而不是.Skipskip,请尝试这两种方法,并查看使用SQL探查器或其他查询监视工具生成的SQL。您将在参数化查询中看到.Skip=>Skip结果,而.Skipskip.Taketake将烘焙Skip的值并将其放入查询文本中,这将减少对SQL计划缓存的点击,从而导致分页时性能较差。

您能否提供ApplicationDbContext的相关部分代码,这是一个完整的例子。并更改user和list的类型声明,以便清楚地知道它们是什么类型。无法从代码中确定这些变量的类型。Skip和Take在您发布的ApplicationUser类的ApplicationUserNotifications属性中可用。@Mick哦,没错,ICollection实现了IEnumerable,因此Skip和Take应该可用。尽管如此,它仍会导致在内存中拉取整个集合。@Stijn可以使用Skip and Take-in实体框架,而无需拉取整个集合memory@Mick但在使用导航属性时不会,对吗?
DbSet<ApplicationUserNotification>()
    .Where(n => n.ApplicationUserId == User.Identity.GetUserId())
    .Skip(() => skip)
    .Take(() => take)
    .ToList()