C# 无法将integer类型隐式转换为bool
希望你能在这个问题上帮助我。我之前搜索过一些问题,但是 无法使用提供的答案解决此问题。 我在where子句中得到错误“不能隐式地将integer类型转换为bool”。 此时C# 无法将integer类型隐式转换为bool,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,希望你能在这个问题上帮助我。我之前搜索过一些问题,但是 无法使用提供的答案解决此问题。 我在where子句中得到错误“不能隐式地将integer类型转换为bool”。 此时 数据库中的SchoolMasterID列和通过构造函数提供的Id都 具有整数类型 public List<SchoolmasterIndexView> GetdSchoolMaster(EduDbContext db, int Id) { var _schoolmaster = from n in db.S
数据库中的SchoolMasterID列和通过构造函数提供的Id都
具有整数类型
public List<SchoolmasterIndexView> GetdSchoolMaster(EduDbContext db, int Id)
{
var _schoolmaster = from n in db.SchoolMasters
join c in db.Address on n.AddressID equals c.AddressID
where **n.SchoolMasterID = Id**
orderby n.SchoolMasterName
select new SchoolmasterIndexView()
{
SchoolMasterID = n.SchoolMasterID,
SchoolMasterName = n.SchoolMasterName,
AddressID = c.AddressID,
AddressLine1 = c.AddressLine1,
AddressLine2 = c.AddressLine2,
PostalCode = c.PostalCode,
City = c.City
};
return _schoolmaster.ToList();
}
}
public List GetdSchoolMaster(EduDbContext db,int-Id)
{
var_schoolmaster=从n开始,单位为db.schoolmaster
在n.AddressID上的db.Address中加入c等于c.AddressID
其中**n.SchoolMasterID=Id**
校名
选择新的SchoolmasterIndexView()
{
SchoolMasterID=n.SchoolMasterID,
SchoolMasterName=n.SchoolMasterName,
AddressID=c.AddressID,
AddressLine1=c.AddressLine1,
AddressLine2=c.AddressLine2,
PostalCode=c.PostalCode,
城市
};
return_school master.ToList();
}
}
您需要在where
子句中使用相等运算符而不是赋值运算符
var _schoolmaster = from n in db.SchoolMasters
join c in db.Address on n.AddressID equals c.AddressID
where n.SchoolMasterID == Id //<--equality operator
orderby n.SchoolMasterName
...
编译器触发完全相同的错误无法将类型“int”隐式转换为“bool”
出现此错误,因为赋值运算符返回赋值。例如:
int a;
var b = a = 2; //result of "a = 2" expression is 2 and then assigned to b
Console.WriteLine(b); //prints 2. Two variables both have value 2
在Linq
where
中,子句期望bool
但获取int
类型。这就是为什么编译器抱怨说,它不能将int
转换为bool您需要在where
子句中使用相等运算符而不是赋值运算符
var _schoolmaster = from n in db.SchoolMasters
join c in db.Address on n.AddressID equals c.AddressID
where n.SchoolMasterID == Id //<--equality operator
orderby n.SchoolMasterName
...
编译器触发完全相同的错误无法将类型“int”隐式转换为“bool”
出现此错误,因为赋值运算符返回赋值。例如:
int a;
var b = a = 2; //result of "a = 2" expression is 2 and then assigned to b
Console.WriteLine(b); //prints 2. Two variables both have value 2
在Linqwhere
中,子句期望bool
但获取int
类型。这就是为什么编译器抱怨它不能将int
转换为bool