加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用后丢弃IQueryable

发布时间:2020-12-15 08:37:53 所属栏目:百科 来源:网络整理
导读:我有一些看起来像这样的短代码: public static IQueryableUser SelectFromEmployee(int employee){ using (var ctx = Database.AccountingContext()) { return ctx.Users.Where(c = c.Employee_FK == employee); }} 如果我只是保持这个代码,并使用结果我得
我有一些看起来像这样的短代码:
public static IQueryable<User> SelectFromEmployee(int employee)
{
    using (var ctx = Database.AccountingContext())
    {
        return ctx.Users.Where(c => c.Employee_FK == employee);
    }
}

如果我只是保持这个代码,并使用结果我得到异常告诉我数据被处置.但如果我喜欢这样:

public static IEnumerable<User> SelectFromEmployee(int employee)
{
    using (var ctx = Database.AccountingContext())
    {
        return ctx.Users.Where(c => c.Employee_FK == employee).ToList();
    }
}

一切正常.但我的问题是我想使用需要IQueryable的Linq Dynamic.有没有办法返回本地IQueryable所以我可以继续使用它?

解决方法

如果你想真正做你所要求的
public static IQueryable<User> SelectFromEmployee(int employee)
{
    using (var ctx = Database.AccountingContext())
    {
        return ctx.Users.Where(c => c.Employee_FK == employee).ToList().AsQueryable();
    }
}

请注意,虽然您返回的内容不再与数据上下文相关联,但是例如,如果未处理数据上下文,则关系导航将无法正常工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读