简体   繁体   English

无法转换类型&#39;System.Linq.IQueryable <int> &#39;到&#39;int&#39;

[英]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.

相关问题 无法将类型'System.Linq.IQueryable <int>'隐式转换为'int?' - Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int?' 如何转换system.linq.IQueryable <int?> 诠释?[] - how to convert system.linq.IQueryable<int?> to int?[] 错误:实例参数:无法从&#39;int&#39;转换为&#39;System.Linq.IQueryable <int> “ - Error :Instance argument: cannot convert from 'int' to 'System.Linq.IQueryable<int>' 运算符&#39;==&#39;不能应用于&#39;int&#39;和&#39;System.Linq.iQueryable类型的操作数 <int> “ - Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.iQueryable<int>' c#-Linq错误“运算符&#39;==&#39;无法应用于类型为&#39;int&#39;和&#39;System.Linq.IQueryable的操作数 <T> ”和RemoveRange - c# - Linq Error “Operator '==' cannot be applied to operands of type 'int' and 'System.Linq.IQueryable<T>” and RemoveRange 无法隐式转换类型&#39;System.Linq.IQueryable <AnonymousType#1> &#39;到&#39;System.Linq.IQueryable &lt;&gt;&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<>' 无法转换类型&#39;System.Linq.IQueryable <double?> &#39; 漂浮&#39; - Cannot convert type 'System.Linq.IQueryable<double?>' to 'float' 无法隐式转换System.Linq.IQueryable类型 <string> 串起来 - Cannot implicitly convert type System.Linq.IQueryable<string> to string 无法隐式转换类型&#39;System.Linq.IQueryable? - Cannot implicitly convert type 'System.Linq.IQueryable? MVC 5无法将类型&#39;System.Linq.IQueryable隐式转换为bool吗? - MVC 5 Cannot implicitly convert type 'System.Linq.IQueryable to bool?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM