我想连接两个表,并希望使用linq
在结果中使用两个表的列我有类似
的东西var k = (from t in Uow.Transactions.GetAllWithReferences()
join q in Uow.TransactionDetails.GetAll() on t.TransactionId equals q.TransactionId
select t)
而不仅仅是你和t和q的列。
答案 0 :(得分:1)
使用匿名对象从两个表中选择所有列:
var k = from t in Uow.Transactions.GetAllWithReferences()
join q in Uow.TransactionDetails.GetAll()
on t.TransactionId equals q.TransactionId
select new { t.Column1, t.Column2, q.Colum3 };
或者只选择匿名对象中的两个实体:
var k = from t in Uow.Transactions.GetAllWithReferences()
join q in Uow.TransactionDetails.GetAll()
on t.TransactionId equals q.TransactionId
select new { Transaction = t, Details = q };
甚至更好 - 使用eager loading详细信息。然后代码如下:
var k = Uow.Transactions.GetAllIncluding(t => t.Details);
答案 1 :(得分:1)
使用lambda语法,创建包含两个项目的匿名结果:
var joinResult = Uow.Transactions.GetAllWithReferences()
.Join(Uow.TransactionDetails.GetAll(), transaction => transaction,
details => details, (transaction, details) => new
{
Transaction = transaction,
Details = details
});