Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 转换型系统。Linq.IQueryable<;int>;到int?_C#_Linq_Int - Fatal编程技术网

C# 转换型系统。Linq.IQueryable<;int>;到int?

C# 转换型系统。Linq.IQueryable<;int>;到int?,c#,linq,int,C#,Linq,Int,我目前正在为一个变量分配数据库表中ID的值 var customerID = (from c in db.Customers select c.CustomerID); 之后,我希望通过If语句将ID的值与用户的ID相等。然而,我收到: 无法将类型“System.Linq.IQueryable”隐式转换为“int” 如何将IQueryable转换为int??问题是您正在选择客户表的所有ID,请使用扩展方法或: 将.FirstOrDefault()添加到查询的

我目前正在为一个变量分配数据库表中ID的值

var customerID = (from c in db.Customers
                 select c.CustomerID); 
之后,我希望通过If语句将ID的值与用户的ID相等。然而,我收到:

无法将类型“System.Linq.IQueryable”隐式转换为“int”


如何将
IQueryable
转换为
int?

问题是您正在选择
客户表的所有ID,请使用扩展方法或:

将.FirstOrDefault()添加到查询的末尾。
var yourId=1;
//...
var customer = db.Customers.FirstOrDefault(c=>c.CustomerId==yourId);