背景介绍:
在无主外键关系的表中如果如果要关联就要用Join()和GroupJoin()方法了,我们先看Join()方法,代码如下:
static void Main(string[] args)
{List<SW_XSDD> sw_xsdd = new List<SW_XSDD>(){new SW_XSDD { com_id = "SH01",vou_no = "100",corr_id = "A",corr_date = DateTime.Now},new SW_XSDD { com_id = "SH02",vou_no = "200",corr_id = "B",corr_date = DateTime.Now.AddDays(1)},new SW_XSDD { com_id = "SH03",vou_no = "300",corr_id = "C",corr_date = DateTime.Now.AddDays(2)}};List<SW_XSDD_C> sw_xsdd_c = new List<SW_XSDD_C>(){new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700001",number = 10,vou_line_no = 1},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700002",number = 11,vou_line_no = 2},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700003",number = 12,vou_line_no = 3},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700004",number = 13,vou_line_no = 4},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700005",number = 14,vou_line_no = 5},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700006",number = 15,vou_line_no = 6},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700007",number = 16,vou_line_no = 1},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700008",number = 17,vou_line_no = 2},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700009",number = 18,vou_line_no = 3},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700010",number = 19,vou_line_no = 4},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700011",number = 20,vou_line_no = 5},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700012",number = 21,vou_line_no = 6},};var Lists = sw_xsdd.Join(sw_xsdd_c, p => new { com_id = p.com_id, vou_no = p.vou_no }, q => new { com_id = q.com_id, vou_no = q.vou_no }, (p, q) => new { com_id = p.com_id, vou_no = p.vou_no, vou_line_no = q.vou_line_no, corr_id = p.corr_id, corr_date = p.corr_date, item_no = q.item_no, number = q.number });foreach (var list in Lists){Console.WriteLine($"{list.com_id}-{list.vou_no}-{list.vou_line_no}-{list.item_no}-{list.number}-{list.corr_id}-{list.corr_date}");}Console.Read();
}public class SW_XSDD
{public string com_id { get; set; }public string vou_no { get; set; }public string corr_id { get; set; }public DateTime corr_date { get; set; }
}public class SW_XSDD_C
{public string com_id { get; set; }public string vou_no { get; set; }public int vou_line_no { get; set; }public string item_no { get; set; }public int number { get; set; }
}
上述代码中SW_XSDD实体类和SW_XSDD_C实体类通过 com_id和vou_no 这两个字段关联,在SW_XSDD中存在一条com_id为SH03的数据,这条数据在SW_XSDD_C中因为没有对应数据是无法关联到的,运行结果如下所示:
从运行结果中得知,Join()方法应该是SQL中INNER JOIN,SW_XSDD_C中没有的数据就不显示了。
下面再看GroupJoin()方法,代码如下:
public class SW_XSDD
{public string com_id { get; set; }public string vou_no { get; set; }public string corr_id { get; set; }public DateTime corr_date { get; set; }
}public class SW_XSDD_C
{public string com_id { get; set; }public string vou_no { get; set; }public int vou_line_no { get; set; }public string item_no { get; set; }public int number { get; set; }
}
static void Main(string[] args)
{List<SW_XSDD> sw_xsdd = new List<SW_XSDD>(){new SW_XSDD { com_id = "SH01",vou_no = "100",corr_id = "A",corr_date = DateTime.Now},new SW_XSDD { com_id = "SH02",vou_no = "200",corr_id = "B",corr_date = DateTime.Now.AddDays(1)},new SW_XSDD { com_id = "SH03",vou_no = "300",corr_id = "C",corr_date = DateTime.Now.AddDays(2)}};List<SW_XSDD_C> sw_xsdd_c = new List<SW_XSDD_C>(){new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700001",number = 10,vou_line_no = 1},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700002",number = 11,vou_line_no = 2},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700003",number = 12,vou_line_no = 3},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700004",number = 13,vou_line_no = 4},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700005",number = 14,vou_line_no = 5},new SW_XSDD_C { com_id = "SH01",vou_no = "100",item_no = "101700006",number = 15,vou_line_no = 6},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700007",number = 16,vou_line_no = 1},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700008",number = 17,vou_line_no = 2},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700009",number = 18,vou_line_no = 3},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700010",number = 19,vou_line_no = 4},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700011",number = 20,vou_line_no = 5},new SW_XSDD_C { com_id = "SH02",vou_no = "200",item_no = "101700012",number = 21,vou_line_no = 6},};var Lists = sw_xsdd.GroupJoin(sw_xsdd_c, p => new { com_id = p.com_id, vou_no = p.vou_no }, q => new { com_id = q.com_id, vou_no = q.vou_no }, (p, k) => new { com_id = p.com_id, vou_no = p.vou_no,corr_id = p.corr_id, corr_date = p.corr_date,rest = k });foreach (var list in Lists){Console.WriteLine($"{list.com_id}-{list.vou_no}-{list.corr_id}-{list.corr_date}");foreach (var l in list.rest){Console.WriteLine($"{l.vou_line_no}-{l.item_no}-{l.number}");}}Console.Read();
}
在这句代码中:
var Lists = sw_xsdd.GroupJoin(sw_xsdd_c, p => new { com_id = p.com_id, vou_no = p.vou_no }, q => new { com_id = q.com_id, vou_no = q.vou_no }, (p, k) => new { com_id = p.com_id, vou_no = p.vou_no,corr_id = p.corr_id, corr_date = p.corr_date,rest = k });
K为类型为:,所以无法直接取出SW_XSDD_C中特有的字段,需要用到双重循环实现,运行结果如下所示:
可以看到SW_XSDD中com_id为SH03的数据显示出来了,只是没有SW_XSDD_C中的数据,类似于SQL 中的 LEFT JOIN。