Linq to sql LINQ到SQL:具有计数(表id)

Linq to sql LINQ到SQL:具有计数(表id),linq-to-sql,asp.net-mvc-2,Linq To Sql,Asp.net Mvc 2,我正在为一所学校建立一个网站,我需要一个没有双亲的用户列表。这将是SQL查询: SQL 我试图使用LINQ进行相同的查询,但我不知道如何使用HAVING和LINQ。这是我目前最接近的尝试 中的子查询 List<long> usersWithTwoParentsIds = (from currentStudents in contexto.user_parents select currentStudents.parent_user_id).ToList<long>();

我正在为一所学校建立一个网站,我需要一个没有双亲的用户列表。这将是SQL查询:

SQL

我试图使用LINQ进行相同的查询,但我不知道如何使用HAVING和LINQ。这是我目前最接近的尝试

中的子查询

List<long> usersWithTwoParentsIds = (from currentStudents in contexto.user_parents
select currentStudents.parent_user_id).ToList<long>();
--HELP! having count(currentStudents.parent_user_id) < 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();

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

var dat = Context.Users.Where( u => u.Parents.Count() < 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();
var dat = Context.Users.Where( u => u.Parents.Count() < 2 );