我在将数据从数据库发送到Controller中的列表时遇到问题.我是C#和MVC的新手,所以任何帮助都会有用!代码如下
public static ListGetAllUsers() { FilterTreeDBContext db = new FilterTreeDBContext(); var userList = new List (); 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查询是惰性的,只有在请求值后才会执行
BTW不要忘记关闭上下文,否则你会泄漏连接
using (var db = new FilterTreeDBContext()) { ... return userList; }