[英]Cannot convert type 'System.Linq.IQueryable<int>' to 'int'
I have a problem sending data from the database to a list in the Controller. 我在将数据从数据库发送到Controller中的列表时遇到问题。 I am new to C# and MVC so any help will be useful!
我是C#和MVC的新手,所以任何帮助都会有用! Code follows
代码如下
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;
}
Thanks & Happy Holidays!! 谢谢,节日快乐! :)
:)
device.ID = (from a in db.FilterTree select a.ID).First();
Linq query is lazy, and executes only after you request the value Linq查询是惰性的,只有在请求值后才会执行
BTW don't forgot to close the context, otherwise you will leak connections BTW不要忘记关闭上下文,否则你会泄漏连接
using (var db = new FilterTreeDBContext())
{
...
return userList;
}
至于框架知道查询可能有多个对象,所以像这样使用它:
device.ID = (from a in db.FilterTree select a.ID).FirstOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.