我经常探索,并且知道在LINQ中没有直接的方法来应用左连接。所以我知道使用交叉连接,然后过滤记录。
我尝试应用它,但仍然没有从左边获取所有记录。
这是我的试用版:
List<UserAttendanceHistory> lstUserAttendanceHistory = (from registeredAppUser in registeredAppUsers
from userAttendanceHistory in lstUserAttendance.DefaultIfEmpty()
where registeredAppUser.EmailID == userAttendanceHistory.EmailID
//on userAttendanceHistory.EmailID equals registeredAppUser.EmailID
select new UserAttendanceHistory()
{
ContactName = registeredAppUser.ContactName,
EmailID = userAttendanceHistory.EmailID,
Status = userAttendanceHistory.Status
}).ToList();
我在lstUserAttendance中有6条记录,在registeredAppUsers中有11条记录。我希望输出中来自registeredAppUsers的所有11条记录。
我知道有很多类似的问题,但我发布了这个因为我无法弄明白。
非常感谢任何帮助。
答案 0 :(得分:2)
有点令人费解。我的方法是进行组连接,其中右侧(通用段或用户)是键,左侧(条件段或出勤历史)是分组元素。 SelectMany然后将列表列表投射到单个结果中。
var userAttendanceHistory =
registeredAppUsers.GroupJoin(lstUserAttendance,
rAU=>rAu.EmailID,
lUA=>lUA.EmailId,
(u,a) => new {user=u, attend=a})
.SelectMany(anonType=> anonType.DefaultIfEmpty(),
(us,at) => new UserAttendanceHistory() {ContactName = us.ContactName,
EmailID = us.EmailID,
Status = at == null ? "Not attended": at.Status
}).ToList();
答案 1 :(得分:1)
如join clause (C# Reference)中所述,LINQ中的左连接模式就像这样
var lstUserAttendanceHistory = (
from registeredAppUser in registeredAppUsers
join userAttendanceHistory in lstUserAttendance
on registeredAppUser.EmailID equals userAttendanceHistory.EmailID
into registeredAppUserHistory
from userAttendanceHistory in registeredAppUserHistory.DefaultIfEmpty()
select new UserAttendanceHistory()
{
ContactName = registeredAppUser.ContactName,
EmailID = userAttendanceHistory.EmailID,
Status = userAttendanceHistory.Status
})
.ToList();
将其应用于您的查询:
class Theme {
// static constant
static let foo = "foo"
// static variable
static var foo2 : String { return "foo2" }
// static method
class func bar(x: Int) -> Int
{
return 2 * x
}
}
let a = Theme.foo // "foo"
let b = Theme.foo2 // "foo2"
let y = Theme.bar(10) // 20