c# - 无法将类型 'System.Linq.IQueryable<int>' 转换为 'int'

标签 c# database asp.net-mvc-4 controller

我在将数据从数据库发送到 Controller 中的列表时遇到问题。我是 C# 和 MVC 的新手,所以任何帮助都会很有用!代码如下

public static List<FilterTree> GetAllUsers()
    {
        FilterTreeDBContext db = new FilterTreeDBContext();
        var userList = new List<FilterTree>();
        var device =  new FilterTree();
        //This is the line where I get the error
        device.ID = from a in db.FilterTree select a.ID;

        userList.Add(device);
        return userList;
    }

谢谢,节日快乐!! :)

最佳答案

device.ID = (from a in db.FilterTree select a.ID).First();

Linq 查询是惰性的,只有在您请求值后才执行

顺便说一句,不要忘记关闭上下文,否则你会泄漏连接

using (var db = new FilterTreeDBContext())
{
    ...
    return userList;
}

关于c# - 无法将类型 'System.Linq.IQueryable<int>' 转换为 'int',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20660586/

相关文章:

mysql - 存储琐事替代品的最佳方式?

c# - 从内部网络中的另一台机器访问 Docker 容器

c# - 垃圾收集器世代不递增

c# - 选择符合特定条件的 child 的 parent

c# - 每个 session 存储大量数据 ASP.NET MVC

asp.net-mvc - 如何在 cshtml 中停止 ASP.NET MVC View 渲染?

javascript - ASP.net MVC 的 Kendo UI 网格

c# - FluentValidation:将值与值的集合进行比较?

mysql - 设计数据库表以在允许插入之前检查另一个表中的ID

database - 为 SEO 友好的 URL 优化数据库