Linq to Entities 不同类型的左外连接

发布于 2024-11-06 23:08:30 字数 1736 浏览 0 评论 0原文

到目前为止,我可能已经在这个问题上花了 40 个小时,我已经尝试了这个网站和谷歌上的所有解决方案,但我仍然无法完成这项工作。

我需要将表左连接到先前查询的结果,存储在 var 中。连接字段在查询 var 中结果的表中是 varchar,在被连接的表中是 bigint(long)。这是当前的尝试,它告诉我“对象引用未设置到对象的实例”。所有实体错误对我来说似乎都是无稽之谈和谎言,我认为它试图告诉我没有匹配的内容,但谁知道呢。

        List<reportUser> ru = leaders
        .GroupJoin(db.sweeps,
        a => a.FBID.ToString(),
        s => s.userFBID.First().ToString(),
        (a, matching) => new reportUser
        {
            FBID = a.FBID,
            CurrentPoints = a.CurrentPoints,
            Name = matching.FirstOrDefault().Name,
            Email = matching.FirstOrDefault().email
        }
        ?? new reportUser
        {
            FBID = 0,
            CurrentPoints = 0,
            Name = "",
            Email = ""
        })
        .Select(a => a)
        .ToList();

这是下面请求的 SQL。我还包含了用于构建 Leaders 对象的 SQL,以上所有内容实际上代表的是最后一行,这只是一个左连接。

总体选择 s.name、s.email、b.score、c.score 从 ( 选择a.userfbid, sum(a.pointvalue) 分数 从 ( 选择用户出价、点值 从我 在 qa.id = l.qaid 上左加入 qa 在 q.id = qa.qid 上左连接 q 在 qz.id = q.qzid 上左连接 qz 其中 qa.pointvalue > 0 和 qz.cid = 12 联合所有 选择 fbid userfbid, 点值 来自 BN 其中日期 >= '5/9/2011 04:00' 并且 日期 <= '5/16/2011 04:00' ) 一个 按 a.userfbid 分组 ) b

左连接 ( 选择a.userfbid, sum(a.pointvalue) 分数 从 ( 选择用户出价、点值 从我 在 qa.id = l.qaid 上左加入 qa 在 q.id = qa.qid 上左连接 q 在 qz.id = q.qzid 上左连接 qz 其中 qa.pointvalue > 0 联合所有 选择 fbid userfbid, 点值 来自 BN ) 一个 按 a.userfbid 分组 ) c on c.userfbid=b.userfbid

left join s on s.userfbid=b.userfbid 按分数排序

I've probably spent 40 hours on this problem so far, I've tried every solution on this site and on google, and I still can't make this work.

I need to left join a table to the results of a previous query, stored in a var. The joining field is a varchar in the table queried for the result in the var, and a bigint (long) in the table being joined. Here is the current attempt, which tells me "Object reference not set to an instance of an object." All Entities errors seem like nonsense and lies to me, I assume it's trying to tell me nothing matched, but who knows.

        List<reportUser> ru = leaders
        .GroupJoin(db.sweeps,
        a => a.FBID.ToString(),
        s => s.userFBID.First().ToString(),
        (a, matching) => new reportUser
        {
            FBID = a.FBID,
            CurrentPoints = a.CurrentPoints,
            Name = matching.FirstOrDefault().Name,
            Email = matching.FirstOrDefault().email
        }
        ?? new reportUser
        {
            FBID = 0,
            CurrentPoints = 0,
            Name = "",
            Email = ""
        })
        .Select(a => a)
        .ToList();

Here's the SQL requested below. I've included the SQL to build the Leaders object as well, all the above is really meant to represent is the last line, which is simply a left join.

select s.name, s.email, b.score, c.score overall
from (
select a.userfbid, sum(a.pointvalue) score
from (
select userfbid, pointvalue
from l
left join qa on qa.id = l.qaid
left join q on q.id = qa.qid
left join qz on qz.id = q.qzid
where qa.pointvalue > 0 and qz.cid = 12
union all
select fbid userfbid, pointvalue
from bn
where date >= '5/9/2011 04:00' and
date <= '5/16/2011 04:00'
) a
group by a.userfbid
) b

left join (
select a.userfbid, sum(a.pointvalue) score
from (
select userfbid, pointvalue
from l
left join qa on qa.id = l.qaid
left join q on q.id = qa.qid
left join qz on qz.id = q.qzid
where qa.pointvalue > 0
union all
select fbid userfbid, pointvalue
from bn
) a
group by a.userfbid
) c on c.userfbid=b.userfbid

left join s on s.userfbid=b.userfbid
order by score desc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻拂→两袖风尘 2024-11-13 23:08:30

我假设在您的数据库中 s.userFBID.First() 永远不会为空?

如果这是正确的,那么您的问题可能出在 FirstOrDefault().Name 类型语句中 - 当 FirstOrDefault() 计算结果为 null 时,显然您会得到一个 nullreferenceexception :/

要解决这个问题,请尝试类似的方法:

List<reportUser> ru = leaders
        .GroupJoin(db.sweeps,
        a => a.FBID.ToString(),
        s => s.userFBID.First().ToString(),
        (a, matching) => 
        {
            var match = matching.FirstOrDefault();
            return match != null ?
            new reportUser
        {
            FBID = a.FBID,
            CurrentPoints = a.CurrentPoints,
            Name = match.Name,
            Email = match.email
        }
        : new reportUser
        {
            FBID = 0, // a.FBID ?
            CurrentPoints = 0, // a.CurrentPoints ?
            Name = "",
            Email = ""
        }})
        .Select(a => a)
        .ToList();

但是,我发现在没有看到数据库的结构或一些示例数据的情况下执行此操作有点困难...


一旦你有了一些工作...那么我强烈建议你尝试打破这个变成更容易理解的东西 - 我真的不确定这里发生了什么!

I'm assuming that in your database s.userFBID.First() is never null?

If that's right, then your problem could be in the FirstOrDefault().Name type statements - when FirstOrDefault() evaluates to null then obviously you will get a nullreferenceexception :/

To get around this, try something like:

List<reportUser> ru = leaders
        .GroupJoin(db.sweeps,
        a => a.FBID.ToString(),
        s => s.userFBID.First().ToString(),
        (a, matching) => 
        {
            var match = matching.FirstOrDefault();
            return match != null ?
            new reportUser
        {
            FBID = a.FBID,
            CurrentPoints = a.CurrentPoints,
            Name = match.Name,
            Email = match.email
        }
        : new reportUser
        {
            FBID = 0, // a.FBID ?
            CurrentPoints = 0, // a.CurrentPoints ?
            Name = "",
            Email = ""
        }})
        .Select(a => a)
        .ToList();

However, I find it a bit hard to do this without seeing the structure of the database... or some sample data


Once you've got something working... then I highly recommend you try breaking this down into something more easily understandable - I'm really not sure what's going on here!

花开柳相依 2024-11-13 23:08:30

这是一个简单的左外连接:

var query = from l leaders
        join s in db.sweeps on l.FBID equals s.userFBID.First() into joined
        from j in joined.FirstOrDefault()
        select new reportUser
    {
        FBID = l.FBID,
        CurrentPoints = l.CurrentPoints,
        Name = j == null ? string.Empty : j.Name,
        Email = j == null ? string.Empty : j.email
    }

如果这不完全是您想要的...也许可以尝试发布您真正想要的 SQL。

Here's a simple left outer join for you:

var query = from l leaders
        join s in db.sweeps on l.FBID equals s.userFBID.First() into joined
        from j in joined.FirstOrDefault()
        select new reportUser
    {
        FBID = l.FBID,
        CurrentPoints = l.CurrentPoints,
        Name = j == null ? string.Empty : j.Name,
        Email = j == null ? string.Empty : j.email
    }

If this isn't quite what you are looking for... maybe try posting the SQL for what you actually want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文