我是LINQ的新手,我有一个问题。
我有两个表在两个不同的dbContext(userContext和mailContext)。我想从UserAccount表中选择一部分数据,然后将这些数据添加到UserMail表中。UserAccount表有很多列,UserMail表有4列(CreatedDate, UserAccountId, UserType, ItemCount)
选择代码:
var selectedUsers = _userContext.UserAccount
.Where(x=>x.Created == new DateTime(2021, 02, 17))
.Select(x => new UserMail()
{
CreatedDate = x.Created,
UserAccountId = x.AccountId,
UserType = x.UserType,
ItemCount = (int?)x.ItemCount,
}).ToList();
查询返回null, selectedUsers的计数=0。我检查从SQL,一些数据退出,我错过了一个点在查询?
实际上,我需要按AccountId和CreatedDate分组的ItemCount的总和,但首先,我想得到这个查询的结果。
这意味着您不能满足where
条件,因为极有可能小时/分钟/秒部分彼此不相等。下面这些呢?
var selectedUsers = _userContext.UserAccount
.Where(x=>x.Created.Date == new DateTime(2021, 02, 17).Date)
// new DateTime(2021, 02, 17) could be used simply but the default
// behavior may be changed in future.
.Select(x => new UserMail()
{
CreatedDate = x.Created,
UserAccountId = x.AccountId,
UserType = x.UserType,
ItemCount = (int?)x.ItemCount,
}).ToList();