2011-05-15 46 views
2

我正在建立一所学校的网站,我需要一个没有2位父母签名的用户列表。这将是SQL查询:LINQ TO SQL:有计数(table_id)<2

SQL

SELECT * FROM users 
WHERE user_rol = 4 
AND user_id IN 
(SELECT parent_user_id FROM user_parents 
GROUP BY parent_user_id 
HAVING COUNT(parent_user_id) < 2); 

我试图使用LINQ进行相同的查询,但我不知道如何使用LINQ拥有。这一直是我的近距离尝试。

SUB-查询IN

List<long> usersWithTwoParentsIds = (from currentStudents in contexto.user_parents 
select currentStudents.parent_user_id).ToList<long>(); 
--HELP! having count(currentStudents.parent_user_id) < 2 

QUERY

List<vw_user> userList = (from currentStudents in contexto.vw_user 
where !usersWithTwoParentsIds.Contains(currentStudents.user_id) 
&& currentStudents.group_id == groupID select currentStudents).ToList<vw_user>() 

任何人能提供线索?谢谢:)

回答

2

事情是这样的:

var usersWithTwoParentsIds = (
    from userParent in contexto.user_parents 
    group userParent by userParent.parent_user_id into userParentGroups 
    where userParentGroups.Count() < 2 
    select userParentGroups.Key) 
    .ToList(); 
+0

是塔的完美谢谢:) – 2011-05-15 17:54:26

0

假设的关键关系是在你的数据上下文正确:

var dat = Context.Users.Where(u => u.Parents.Count() < 2);