将 IQueryable 与 MVC 中的字符串进行比较

Compare IQueryable to String in MVC

我这里有这个 IQueryable :

我调用 IQueryable 的方式如下:

 var code = securityCodeCheck(item.SelectedDistrict);

我的比较是这样的:

if (item.DistrictCode == code)
{
     return view();
}

else{
     return RedirectToAction(....);
}

我怎样才能成功比较两者?

对于第一个问题,您正在 selecting District.securityCode(我假设它是一个字符串)

尝试 select District 因为这将是正确的 IQueryable 来修复错误,因为该方法明确地将其定义为 return 类型。

代码更改如下。

var query = from District in db.Districts
 where District.leaID.Equals(district)
select District;

对于问题 2.

您可以如下使用。

var code = securityCodeCheck(item.SelectedDistrict);

if(item.DistrictCode == code.First().securityCode)
{
//do stuff
}