如何使用LinQ lambda表达式编写此查询
我写了这个,但不正确:如何使用LinQ lambda表达式编写此查询,linq,join,lambda,entity,Linq,Join,Lambda,Entity,我写了这个,但不正确: SELECT PropertyCommnets.commnet_content, PropertyCommnets.commnet_date, users.user_name, propertycommnets.commnet_id FROM PropertyCommnets INNER JOIN aspnet_users ON propertycommnets.use
SELECT PropertyCommnets.commnet_content,
PropertyCommnets.commnet_date,
users.user_name,
propertycommnets.commnet_id
FROM PropertyCommnets
INNER JOIN aspnet_users
ON propertycommnets.userid = aspnet_users.userid
INNER JOIN users
ON aspnet_users.userid = users.userid
a) 您加入aspnet_用户有什么具体原因吗?
您没有使用aspnet_users.userid
b) 您的尝试是Linq SQL而不是Lambda表达式
c) 您的用户模型中是否有PropertyCommnet的虚拟集合?如果不是,我会考虑的。然后,您可以简单地调用user.propertycommnets和该特定用户的注释。我不明白您在最后用
选择尝试什么。这是无效的语法,与sql查询相比,FirstOrDefault
也不正确
试试这个:
from a in context.PropertyCommnets
join b in context.aspnet_Users
on a.UserId equals b.UserId
join c in context.Users
on b.UserId equals c.UserId
where a.property_id == PropertyId
select(c.user_name ).FirstOrDefault()
但是,这仍然是查询语法,而不是方法/lambda语法。这有关系吗?With JOIN查询语法更具可读性。“我写了这个,但不正确”对不起?
from a in context.PropertyCommnets
join b in context.aspnet_Users
on a.UserId equals b.UserId
join c in context.Users
on b.UserId equals c.UserId
where a.property_id == PropertyId
select new {
commnet_content = a.commnet_content,
commnet_date = a.commnet_date,
user_name = c.user_name,
commnet_id = a.commnet_id,
};