开发者

LINQ TO SQL: HAVING COUNT(table_id) < 2

I'm building a website for an school and i need a list of users that don't have 2 parents asigned. This would be the SQL query:

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);

I'm trying to use LINQ for the same query but I don't know how to use HAVING w开发者_C百科ith LINQ. This has been my closer try for the moment.

SUB-QUERY FOR 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>()

Can anybody give a clue? Thanks :)


Something like this:

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();


Assuming the key relations are proper in your data context:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜