在使用LightSpeed对数据库进行Left Join或Right Join操作时,经常会报一些匪夷所思的异常。
明明表没有问题,表面上语句写的也没问题,可总是报错。看分析器里的SQL就知道了,是LightSpeed对Left Right Join的解析是有问题的。
注:LS对Left Join和 Right Join 最后都会解析成Group Join来处理
当然这种情况只是偶尔发生,是因为某些表及关系的创建方式问题(LS官网要求把项目发给他们来分析问题,嫌麻烦 就没有继续下去,具体原因就没有了解)
现在,可以用另外一种方式来绕过这个Bug(用FindBySQL或者存储过程也可以,但是我不想用SQL语句处理,因为表结构发生变化后,不能第一时间发现问题)
Query query2 = new Query{ EntityType = typeof(LQ_StudentApply), QueryExpression = Entity.Attribute("LQ_SchoolId") == SchoolId, Order = Order.By("CreatedOn").Descending()};//通过OuterJoin来处理Left Join和Right Joinquery2.Join = Join.Outer(typeof(LQ_StudentApply), typeof(LQ_Member), "Operator", "Id");var rst1 = dbPlatform.Find(query2, query2.Mappings);var saList = rst1.GetCollection ();var oList = rst1.GetCollection ();//Inner Joinquery2.Join = Join.Inner(typeof(LQ_StudentApply), typeof(LQ_Member), "LQ_MemberId", "Id");var rst2 = dbPlatform.Find(query2, query2.Mappings);var memList = rst2.GetCollection ();//拼合三个表的数据var query = saList.Zip(memList, (sa, mem) => new{ SA = sa, Mem = mem}).Zip(oList, (mix, o) => new{ MemberId = mix.Mem.Id, SId = mix.SA.Id, OperatorName = o != null ? o.RealName : ""});
解析出来的SQL语句大体就是
SELECT *FROM LQ_StudentApply SALEFT JOIN LQ_Member M ON SA.Operator = M.IdINNER JOIN LQ_Member MEM ON SA.LQ_MemberId = MEM.IdWHERE SA.LQ_SchoolId = 'SchoolId'ORDER BY SA.CreatedOn DESC