2011-05-11 63 views
0

我可能花了40个小时对这个问题到目前为止,我已经尝试了每个解决方案在这个网站和谷歌,我仍然无法做到这一点。Linq实体左外部加入不同类型

我需要左表连接一个表格以查询以前的结果,存储在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请求下面。我已经包含了SQL来构建Leaders对象,上述所有内容都代表最后一行,它只是一个左连接。

选择s.name,s.email,b.score,c.score整体 从( 选择a.userfbid,总和(a.pointvalue)从升 左评分从( 选择userfbid,pointvalue 加入QA上qa.id = l.qaid 留在q.id = qa.qid 加入q留在qz.id = q.qzid 加入QZ其中qa.pointvalue> 0且qz.cid = 12 UNION ALL 选择fbid userfbid,点值 from bn where date> ='5/9/2011 04:00'and date < ='5/16/2011 04: 00' )一个 组由a.userfbid )b

左加入( 选择a.userfbid,总和(a.pointvalue)评分从( 选择userfbid,从升 pointvalue 留在加入QA qa.id = l.qaid 留在q.id = qa.qid 加入q留在qz.id = q.qzid 加入QZ其中qa.pointvalue> 0 UNION ALL 选择FBID userfbid,pointvalue 从BN )a group by a.userfbid )对c.userfbid = b.userfbid

左C按分数降序

回答

0

我假设在你的数据库s.userFBID.First()永远不能为null加入上s.userfbid = b.userfbid s阶?

如果这是正确的,那么你的问题可能是在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(); 

不过,我觉得这是一个有点难以做到这一点没有看到数据库的结构......或者一些示例数据


一旦你有一些工作......然后我强烈建议你尝试把它分解成更容易理解的东西 - 我真的不知道这里发生了什么!

+0

正确,扫描中应始终有一个值以匹配领导者数据中的任何值。尽管领导者可能在价值扫描中没有价值,但这并不重要。 我试过使用.First()和.FirstOrDefault()都给出错误。如果我删除该语句,则会出现无法推断类型参数的错误。 – iniquity 2011-05-11 21:08:39

+0

我想要做的只是一个简单的左连接。我不明白为什么这么多代码甚至是必要的。 我已经在领导变种中有一个userFBID字段,并且在db.Sweeps中有一个匹配字段。我只是想将Sweeps中的名称和电子邮件与领导者中的数据进行匹配。这将在SQL中最多10分钟,但我在实体中处于40小时,所有这些复杂的代码我几乎无法理解,而且我无法修复这些错误。 – iniquity 2011-05-11 21:08:57

+0

您提供的代码将为每个记录返回值为0或“”的空白集。我只是想要它返回,如果它得到一个null,所以下一步不会崩溃。尽管它不应该实际得到一个空值。 – iniquity 2011-05-11 21:13:40

0

这里有一个简单的左外连接您:

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为你真正想要的东西。

+0

因为它不会让我重新声明领导。我假设你打算让它说“来自领导者”,所以我补充说。现在我在“joined.FirstOrDefault()”上得到一个错误,该错误表示“源类型为System.Collections.Generic.IEnumerable '。在对'SelectMany'的调用中,类型推断失败 – iniquity 2011-05-11 21:31:25

+0

我认为是时候花这个“10分钟”编写SQL – Stuart 2011-05-12 06:51:41

+0

将SQL添加到我的原始文章中 – iniquity 2011-05-12 19:47:57