Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用LinQ lambda表达式编写此查询_Linq_Join_Lambda_Entity - Fatal编程技术网

如何使用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,
};