简体   繁体   中英

Linq to Entities Left Outer Join with different types

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

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM