所以我有这个:
db.Table1
.Join(db.Table2, t1 => t1.id, t2 => t2.id, (t1, t2) => new { t1, t2 })
但是我需要做的是同时加入一个属性值/键值表以及一个特定的属性,例如:
SELECT * FROM Table1 t1
JOIN Table2 t2 ON t1.id = t2.id AND t2.attributeid = 123
我如何限定那个attributeid = 123部分?
一个Where
子句应满足:
db.Table1
.Join(db.Table2, t1 => t1.id, t2 => t2.id, (t1, t2) => new { t1, t2 })
.Where(x => x.t2.attributeId == 123);
理想情况下,Table1应该具有到Table2的导航属性:(HasOne或HasMany)
单数表2:
var result = db.Table1
.Include(x => x.Table2)
.Where(x => x.Table2.AttributeId == 123);
或Table2s的集合...
var result = db.Table1
.Include(x => x.Table2s)
.Where(x => x.Table2s.Any(t2 => t2.AttributeId == 123);
它将返回包含具有该属性的Table2的任何Table1 ...
或与您要过滤Table2s的集合:
var result = db.Table1
.Where(x => x.Table2s.Any(t2 => t2.AttributeId == 123)
.Select(x => new
{
Table1 = x,
FilteredTable2s = x.Table2s.Where(t2 => t2.AttributeId == 123).ToList()
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句