右联结换汤不换药,这个联结的概念不仅适用于SQL,对象列表也可以
var list = (from objSubject in listSubjectDetail
join objBalance in listBalance on objSubject.AccountSubjectDetail equals objBalance.GameChannelName
into temp
from tt in temp.DefaultIfEmpty()
select new
{
objSubject.AccountSubject,
objSubject.AccountSubjectDetail,
InitialBalance = tt == null ? 0 : tt.Balance,
objSubject.DebitAmount,
objSubject.CreditAmount,
//join为空时的处理
FinalBalance = (tt == null ? 0 : tt.Balance) + objSubject.DebitAmount - objSubject.CreditAmount,
objSubject.JournalEntryTime
}).ToList();
多表连接
var query = from t1 in dt.AsEnumerable()
join t2 in dt2.AsEnumerable()
on t1.Field<int>("Addressid") equals t2.Field<int>("Addressid") into tmp
join t3 in dt3.AsEnumerable()
on t1.Field<int>("userid") equals t3.Field<int>("userid")
from t in tmp.DefaultIfEmpty()
orderby t1.Field<DateTime>("insertdate") descending
select new
{
Name = t1.Field<string>("Name"),
RealName = (t == null) ? string.Empty : t.Field<string>("RealName"),
nickname = t3.Field<string>("nickname")
};