linq到sql查询无法翻译错误

时间:2014-12-17 15:09:01

标签: c# sql-server linq linq-to-sql

以下是查询:

children = (from r in masterDB.mrrel_Limited2s
                            join s in masterDB.mrconso_SnoMed2014_LimitedToDiseaseBranches on r.AUI2 equals s.AUI                            
                            join a in masterDB.tbl_patients_problems_problemId_to_SnoMed_Iteration2_before_doc_final_s on s.SCUI equals a.SnoMedScui into aGroup
                            from aa in aGroup.DefaultIfEmpty()
                            join g in masterDB.tbl_patients_problems_to_snomed_groups_2014s on s.SCUI equals g.SnoMedScui into gGroup
                            from gg in gGroup.DefaultIfEmpty()
                            where r.AUI1.Equals(node.Value.Split(new string[] { " @ " }, StringSplitOptions.RemoveEmptyEntries)[0])
                            &&
                            r.REL.Equals("CHD")
                            select new RadTreeNode(
                                s.AUI + " @ " + s.SCUI + " @ " + aa != null && gg == null ? "1" : "0" + " @ "
                                + gg != null ? gg.GroupName : "0" + " @ " + s.SCUI,
                                s.STR
                                )).ToList();

我正在尝试连接两个满足几个条件的表(底部有两个where子句),然后在另外两个表上设置左连接。我得到一个"无法翻译查询"运行时错误。所有建议表示赞赏。提前谢谢。

2 个答案:

答案 0 :(得分:0)

其中一个主要原因是您使用csharp中的函数,这些函数在SQL中无效。所以你的node.Value.Split就是一个例子。在linq查询之外执行此操作分隔行并将结果数组传递给linq。

var nodePart = node.Value.Split(new string[] { " @ " }, StringSplitOptions.RemoveEmptyEntries)[0]

children = (from r in masterDB.mrrel_Limited2s
                            join s in masterDB.mrconso_SnoMed2014_LimitedToDiseaseBranches on r.AUI2 equals s.AUI                            
                            join a in masterDB.tbl_patients_problems_problemId_to_SnoMed_Iteration2_before_doc_final_s on s.SCUI equals a.SnoMedScui into aGroup
                            from aa in aGroup.DefaultIfEmpty()
                            join g in masterDB.tbl_patients_problems_to_snomed_groups_2014s on s.SCUI equals g.SnoMedScui into gGroup
                            from gg in gGroup.DefaultIfEmpty()
                            where r.AUI1.Equals(nodePart)
                            &&

答案 1 :(得分:0)

问题在于

s.AUI + " @ " + s.SCUI + " @ " + aa != null && gg == null ? "1" : "0" + " @ "
                                + gg != null ? gg.GroupName : "0" + " @ " + s.SCUI

将其更改为:

s.AUI + " @ " + s.SCUI + " @ " + (aa != null && gg == null ? "1" : "0") + " @ "
                                + (gg != null ? gg.GroupName : "0") + " @ " + s.SCUI

它似乎现在正在运作。感谢John帮我解决了另一个问题