按相关表计数过滤查询

时间:2016-05-27 12:47:11

标签: c# entity-framework-6

我正在尝试获取在数据库中至少有一个家庭成员关联的人员列表。

android:fillAlpha是一个相关的表,由一对多的外键绑定。

/var/log/nginx/error.log ------------------------------------- 2016/05/27 11:15:44 [warn] 2797#0: conflicting server name "localhost" on 0.0.0.0:80, ignored 2016/05/27 11:27:26 [crit] 2805#0: *140 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.26.77, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/", host: "viravira-env.bu2eqpbwny.us-west-2.elasticbeanstalk.com" 2016/05/27 11:27:26 [crit] 2805#0: *140 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.26.77, server: _, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/favicon.ico", host: "viravira-env.bu2eqpbwny.us-west-2.elasticbeanstalk.com", referrer: "http://viravira-env.bu2eqpbwny.us-west-2.elasticbeanstalk.com/" 2016/05/27 11:34:45 [crit] 2805#0: *262 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.46.145, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/", host: "viravira-env.bu2eqpbwny.us-west-2.elasticbeanstalk.com" 2016/05/27 11:34:45 [crit] 2805#0: *262 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.46.145, server: _, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/favicon.ico", host: "viravira-env.bu2eqpbwny.us-west-2.elasticbeanstalk.com", referrer: "http://viravira-env.bu2eqpbwny.us-west-2.elasticbeanstalk.com/" 2016/05/27 11:40:48 [crit] 2805#0: *353 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.46.145, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/", host: "viravira-env.bu2eqpbwny.us-west-2.elasticbeanstalk.com" 2016/05/27 11:40:49 [crit] 2805#0: *353 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.46.145, server: _, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/favicon.ico", host: "viravira-env.bu2eqpbwny.us-west-2.elasticbeanstalk.com", referrer: "http://viravira-env.bu2eqpbwny.us-west-2.elasticbeanstalk.com/" ------------------------------------- /var/log/puma/puma.log ------------------------------------- === puma startup: 2016-05-27 11:52:07 +0000 === === puma startup: 2016-05-27 11:52:07 +0000 === [23871] - Worker 0 (pid: 23875) booted, phase: 0 [23871] - Gracefully shutting down workers... [23871] === puma shutdown: 2016-05-27 12:36:32 +0000 === [23871] - Goodbye! === puma startup: 2016-05-27 12:36:35 +0000 === === puma startup: 2016-05-27 12:36:35 +0000 === [24886] - Worker 0 (pid: 24890) booted, phase: 0

我尝试过这样做,但是FamilyMembers似乎没有像我想象的那样工作。 我得到的结果包含0和更多[one] People.PersonId --> [0 or more] FamilyMembers.PersonId

Count()

我真的不确定该怎么做: - /

1 个答案:

答案 0 :(得分:0)

在不使用扩展查询表单的情况下,我努力寻求优雅的解决方案。

我在SQL中实现此目的的唯一方法是使用 Group By Join ,但我不知道我是否正确地将其转换为LINQ。请随时指出我的错误。

var query = Persons
    .Join(FamilyMembers,
        p => p.Id,
        f => f.Id,
        (p,f) => new { Person = p, FamilyMembers = f }
    )
    .GroupBy(g => g.FamilyMembers)
    .Where(w => w.FamilyMembers.Count() > 0);
    .Select(x => new {
        Person = x.Person,
        FamilyMembers = x.Count(y => y.FamilyMembers)
    }
);