[英]left outer join in linq error at defaultifempty
so i have 3 tables i want to do left outer join on all 3 but say 所以我有3个表,我想对所有3个表做左外连接,但要说
table 1 has 表1有
id name
1 peter
2 john
table 2 has 表2有
id address
1 123 ave
2 456 blvd
table 3 has 表3有
table1id table2id
1 1
1 2
2 1
2 2
table 3 is a xref of table 1 and 2 表3是表1和2的外部参照
so if i add one record into table 1, when i search by name, i need to get name and address that name associates with, but it returns nothing because i haven't inserted anything into table 3 yet. 因此,如果我在表1中添加一条记录,则在按名称搜索时,我需要获取与该名称相关联的名称和地址,但是由于我还没有在表3中插入任何内容,因此它不会返回任何内容。 so i'm trying to do a left join but i run into an error
所以我想做一个左联接,但我遇到一个错误
var query = from t1 in table1
join t3 in table3 on t1.id equals t3.table1id into leftt1
from l1 in leftt1.defaultifempty()
join t2 in table2 on l1.table2id equals t2.id
select.......
the error is at defaultifempty. 该错误为defaultifempty。 it actually returns empty instead of the new record i just added.
它实际上返回空而不是我刚刚添加的新记录。 is there something wrong with my left outer join?
我的左外部连接有问题吗? please help.
请帮忙。 thanks
谢谢
Thank you for clarification. 谢谢您的澄清。
Assuming the description of issue, i think you need to use INNER JOIN
instead of LEFT JOIN
, because 3rd table stores data which exist in 1st and 2nd table. 假设问题的描述,我认为您需要使用
INNER JOIN
而不是LEFT JOIN
,因为第三张表存储了第一张表和第二张表中存在的数据。
See example (using LinqPad): 请参阅示例(使用LinqPad):
DataTable dt1 = new DataTable();
DataColumn dc = new DataColumn("id", System.Type.GetType("System.Int32"));
dt1.Columns.Add(dc);
dc = new DataColumn("name", System.Type.GetType("System.String"));
dt1.Columns.Add(dc);
dt1.Rows.Add(new Object[]{1, "Peter"});
dt1.Rows.Add(new Object[]{2, "John"});
DataTable dt2 = new DataTable();
dc = new DataColumn("id", System.Type.GetType("System.Int32"));
dt2.Columns.Add(dc);
dc = new DataColumn("address", System.Type.GetType("System.String"));
dt2.Columns.Add(dc);
dt2.Rows.Add(new Object[]{1, "123 ave"});
dt2.Rows.Add(new Object[]{2, "456 blvd"});
DataTable dt3 = new DataTable();
dc = new DataColumn("id1", System.Type.GetType("System.Int32"));
dt3.Columns.Add(dc);
dc = new DataColumn("id2", System.Type.GetType("System.Int32"));
dt3.Columns.Add(dc);
dt3.Rows.Add(new Object[]{1, 1});
dt3.Rows.Add(new Object[]{1, 2});
dt3.Rows.Add(new Object[]{2, 1});
dt3.Rows.Add(new Object[]{2, 2});
var qry = from refdata in dt3.AsEnumerable()
join userdata in dt1.AsEnumerable() on refdata.Field<int>("id1") equals userdata.Field<int>("id")
join addressdata in dt2.AsEnumerable() on refdata.Field<int>("id2") equals addressdata.Field<int>("id")
select new
{
uid = userdata.Field<int>("id"),
uname = userdata.Field<string>("name"),
aid = addressdata.Field<int>("id"),
aadress = addressdata.Field<string>("address")
};
Result: 结果:
uid uname aid aadress
1 Peter 1 123 ave
1 Peter 2 456 blvd
2 John 1 123 ave
2 John 2 456 blvd
i ended up having to redefined my structure after the join like this 我最终不得不像这样加入后重新定义我的结构
var query = from t1 in table1
join t3 in table3 on t1.id equals t3.table1id into leftt1
from l1 in leftt1.defaultifempty(new t3 {table1id=table1id.id, table2id=0})
join t2 in table2 on l1.table2id equals t2.id
select.......
you can do the same for the other ones as well. 您也可以对其他对象执行相同的操作。 it is working now.
现在正在工作。 thanks
谢谢
You can do this: 你可以这样做:
var query = from t1 in table1s
join t3 in table3s on t1.Id equals t3.table1Id
into left1
from l1 in left1.DefaultIfEmpty()
join t2 in table2s on (l1 == null ? -1 : l1.table2Id) equals t2.Id
into left2
from l2 in left2.DefaultIfEmpty()
select new { t1, l2 };
So you don't need a dummy t3
instance to cross the outer join gap. 因此,您不需要虚拟的
t3
实例即可跨越外部联接间隙。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.