Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在linq中用一列连接多个列_C#_Linq_Join - Fatal编程技术网

C# 在linq中用一列连接多个列

C# 在linq中用一列连接多个列,c#,linq,join,C#,Linq,Join,我需要连接四个表Table1、Table2、Table3和Table4,并需要表1中的所有数据和表2、3和4中的匹配记录。这似乎很简单,但由于表之间的约束,我无法获得正确的查询 Table2&Table3基本上是关联表,包含了Table1&Table4之间的关联。我试图通过LINQ查询获得结果 from t1 in context.Table1 join t2 in context.Table2 on t1.Id equals t2.RefId into t2Temp from t2 in t

我需要连接四个表
Table1、Table2、Table3和Table4
,并需要表1中的所有数据和表2、3和4中的匹配记录。这似乎很简单,但由于表之间的
约束
,我无法获得正确的
查询

Table2
&
Table3
基本上是关联表,包含了
Table1
&
Table4
之间的关联。我试图通过
LINQ
查询获得结果

from t1 in context.Table1

join t2 in context.Table2 on t1.Id equals t2.RefId into t2Temp
from t2 in t2Temp.DefaultIfEmpty()

join t3 in context.Table3 on t1.Id equals t3.RefId into t3Temp
from t3 in t3Temp.DefaultIfEmpty()

join t4 in context.Table4
//here I want all the rows where t4.RefId equals to both t2.RefId & t3.RefId, so that I can get all the associations from table4
on ..... //this is where I got confused how to put the condition
into t4Temp
from t4 in t4Temp.DefaultIfEmpty()

表1和表4之间没有直接参考。我希望这能帮助你理解这个问题。如果需要,我会更乐意添加更多信息。

只要尝试:
在t4上。RefId等于t2。RefId | | t4。RefId等于t3。RefId


然后从您的简短描述中选择t1和t4中的属性

,我猜这类似于上下文中t4中的
。表4中的t4.RefId==t2.RefId | | t4.RefId==t3.RefId
。因此,在第一次与t2连接之后,您有[t1,t2]的组合,其中每个t2都有一个
t2.RefId==t1.Id
。在第二次与T3连接之后,您有组合[T1、T2、T3],其中
T1.Id==T2.RefIds==T3.RefIds
。如果您想将其与所有T4的
T4.RefId等于
T2.RefId和T3.RefId
的T4连接起来,这与在
T4.RefId==T1.Id`上连接不一样吗?毕竟,[T1,T2,T3]组合中的每个RefId都等于T1.Id@格雷特·阿诺德:没错@HaraldCoppoolse是的,但是我没有在Table4的任何列中引用Table1的Id,这是无效的语法。请在发布前测试代码。