Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LINQ查询始终返回Null_Linq_Sql To Linq Conversion - Fatal编程技术网

LINQ查询始终返回Null

LINQ查询始终返回Null,linq,sql-to-linq-conversion,Linq,Sql To Linq Conversion,我是LINQ的新手,我有一个问题 我在两个不同的dbContext(userContext和mailContext)中有两个表。在我将这些数据添加到UserMail表之后,我想从UserAccount表中选择一部分数据。UserAccount表有很多列,UserMail表有4列(CreatedDate、UserAccountId、UserType、ItemCount) 选择代码: var selectedUsers = _userContext.UserAccount .Whe

我是LINQ的新手,我有一个问题

我在两个不同的dbContext(userContext和mailContext)中有两个表。在我将这些数据添加到UserMail表之后,我想从UserAccount表中选择一部分数据。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();

我试过你的解决方案,它不起作用。当我删除where子句时,它正在工作。在数据库中,日期格式为2021-02-17 11:30:35.113。我如何在where子句中转换这个2021-02-17?userContext.UserAccount[0]的输出创建的是2021-01-15 16:23:18.210oov,很抱歉,我没有看到等式两边的.Date。再次感谢,它很有效。