当我将所有内容放在一个语句中时,我有一个完美的查询:
var rs = db.SerialNumbers
.Join(db.ProductLines, sn => sn.ProductLineId, pl => pl.Id, (sn, pl) => new { pl.Id, pl.Category, sn.UserId })
.Where(sn => sn.UserId == userId)
.Select(sn => new { sn.Id, sn.Category })
.Distinct();
Run Code Online (Sandbox Code Playgroud)
但是我需要为UserId添加条件.我只想过滤userId中是否有条目,即userId> 0.所以我改为查询:
var rs = db.SerialNumbers
.Join(db.ProductLines, sn => sn.ProductLineId, pl => pl.Id, (sn, pl) => new { pl.Id, pl.Category, sn.UserId });
if(userId > 0)
{
rs = rs.Where(sn => sn.UserId == userId);
}
rs = rs.Select(sn => new { sn.Id, sn.Category });
Run Code Online (Sandbox Code Playgroud)
我在编译时遇到这个错误:
无法将类型隐式转换
System.Linq.IQueryable<AnonymousType#1> …
这是一个非常愚蠢的问题,但我不知道出了什么问题.我无法通过公共方法获取私有变量的值.我正在使用CodeIgniter.
class someClass extends MY_Model {
private $table = 'sometable';
public function getTable() {
return $this->table;
}
public function updateTable($data) {
$this->db->update($this->getTable(), $data);
}
}
Run Code Online (Sandbox Code Playgroud)
当我从控制器调用此方法时,我收到以下消息:
Fatal error: Access level to someClass::$table must be public (as in class MY_Model) in /some/path/someclass.php on line 38
Run Code Online (Sandbox Code Playgroud)
我做错了什么?谢谢.