我在将数据从数据库发送到 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/