linq连接具有多个列或条件的表

时间:2018-04-22 17:51:40

标签: linq

我想加入3张桌子。两个表通过“或”条件与多个列连接,然后将第三个表连接到前两个结果。

示例如下:

    from t1 in db.table1
    from t2 in db.table2 
    where (t1.ISIN == t2.ISIN_GrowthDiv || t1.ISIN==t2.ISIN_DivReinv)  
    join t3 in table3 on t2.SchemeID equals t3.SchemeId

现在告诉我上面的查询是否正确?

1 个答案:

答案 0 :(得分:0)

由于您没有显示No system resource manager for module: app com.intellij.openapi.diagnostic.Logger$EmptyThrowable at com.intellij.openapi.diagnostic.Logger.error(Logger.java:140) at com.android.tools.idea.uibuilder.property.NlProperties.getPropertiesImpl(NlProperties.java:95) at com.android.tools.idea.uibuilder.property.NlProperties.lambda$getProperties$0(NlProperties.java:83) at com.intellij.openapi.project.DumbService.lambda$runReadActionInSmartMode$0(DumbService.java:94) at com.intellij.openapi.project.DumbService.lambda$runReadActionInSmartMode$1(DumbService.java:138) at com.intellij.openapi.application.ReadAction.compute(ReadAction.java:47) at com.intellij.openapi.project.DumbService.runReadActionInSmartMode(DumbService.java:131) at com.intellij.openapi.project.DumbService.runReadActionInSmartMode(DumbService.java:94) at com.android.tools.idea.uibuilder.property.NlProperties.getProperties(NlProperties.java:83) at com.android.tools.idea.common.property.PropertiesManager.lambda$setSelectedComponents$1(PropertiesManager.java:243) at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:315) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) 条款,我会做一个示例select;当然用你自己的替换它。

我还创建了样本数据,正如您所说,您无法共享,并存储在selecttable1table2中:

table3

这些是我得到的结果:

enter image description here

如你所见,没有交叉联接;这并不奇怪,因为默认情况下LINQ提供了与var table1 = new List<Table1>() { new Table1() { ISIN = 1 }, new Table1() { ISIN = 4 } }; var table2 = new List<Table2>() { new Table2() { ISIN_DivReinv = 0, ISIN_GrowthDiv = 1, SchemeID = 111 }, new Table2() { ISIN_DivReinv = 1, ISIN_GrowthDiv = 0, SchemeID = 111 }, new Table2() { ISIN_DivReinv = 1, ISIN_GrowthDiv = 0, SchemeID = 222 }, new Table2() { ISIN_DivReinv = 2, ISIN_GrowthDiv = 2, SchemeID = 111 } }; var table3 = new List<Table3>() { new Table3() { SchemeId = 111 }, new Table3() { SchemeId = 333 } }; var Result = from t1 in table1 from t2 in table2 where (t1.ISIN == t2.ISIN_GrowthDiv || t1.ISIN == t2.ISIN_DivReinv) join t3 in table3 on t2.SchemeID equals t3.SchemeId select new { t1.ISIN, t2.ISIN_DivReinv, t3.SchemeId }; 运算符的内连接,获得交叉连接的唯一方法就是选择不连接,就像在this回答中一样。

<小时/> 编辑:如果T3.SCHEMEID重复,会怎样?

A&#34;特别&#34; case是join包含重复值的情况,例如像

t3

在这种情况下,您将获得4行而不是2行,因为两行var table3 = new List<Table3>() { new Table3() { SchemeId = 111 }, new Table3() { SchemeId = 111 } }; 两次正确连接,每个匹配值t2一次。

enter image description here

这并没有使它成为一个交叉连接&#34;无论如何,如果你的选择不需要来自t3的值,你可以采用不同的t3.SchemeId值:

t3

enter image description here