我正在尝试构造一个linq方法,我可以使用join和where子句验证两个参数,但是目前我收到了一个编译错误 - 在声明它之前不能使用局部变量'query' - error。
public bool Validate(string username, string password)
{
var query = from t in db.Trial_Try
join u in db.UserDetails on t.tUID equals u.uID
where t.tExpiryDate >= DateTime.Now &&
t.tPublication.Value == 163 &&
u.uUsername == username &&
u.uPassword == password
select query.FirstOrDefault();
}
感谢您的时间和帮助。
答案 0 :(得分:1)
应该是
var query = (from t in db.Trial_Try
join u in db.UserDetails on t.tUID equals u.uID
where t.tExpiryDate >= DateTime.Now &&
t.tPublication.Value == 163 &&
u.uUsername == username &&
u.uPassword == password
select t) .FirstOrDefault();
或者如果你不想那些价值观
基本上你试图在你的select子句中使用查询,这不是它所期望的。
答案 1 :(得分:0)
var query = from t in db.Trial_Try
join u in db.UserDetails on t.tUID equals u.uID
where t.tExpiryDate >= DateTime.Now &&
t.tPublication.Value == 163 &&
u.uUsername == username &&
u.uPassword == password
select t.FirstOrDefault();
答案 2 :(得分:0)
public bool Validate(string username, string password)
{
var query = from t in db.Trial_Try
join u in db.UserDetails on t.tUID equals u.uID
where t.tExpiryDate >= DateTime.Now &&
t.tPublication.Value == 163 &&
u.uUsername == username &&
u.uPassword == password
select u;
// "execute" the query
return query.FirstOrDefault() != null;
}