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中搜索日期范围_Linq_Entity Framework Core - Fatal编程技术网

在LINQ中搜索日期范围

在LINQ中搜索日期范围,linq,entity-framework-core,Linq,Entity Framework Core,我试图通过LINQ和实体框架核心在表中进行搜索。我有两个文本框startdate和enddate,以及一个单选按钮集,其中包括创建、修改和两者 这是我根据谷歌搜索和教程编写的代码 switch(radCreatedModifiedBoth) { case "b": if (!String.IsNullOrEmpty(startDate)) { if (!String.IsNullOrEm

我试图通过LINQ和实体框架核心在表中进行搜索。我有两个文本框startdate和enddate,以及一个单选按钮集,其中包括创建、修改和两者

这是我根据谷歌搜索和教程编写的代码

       switch(radCreatedModifiedBoth) {
            case "b":
                if (!String.IsNullOrEmpty(startDate)) {
                    if (!String.IsNullOrEmpty(endDate)) {
                        persons = persons.Where(ps => (
                                                        ps.CreatedDate >= Convert.ToDateTime(startDate + " 00:00:00") &&
                                                        ps.CreatedDate <= Convert.ToDateTime(endDate + " 23:59:59")
                                                        ) || (
                                                        ps.ModifiedDate >= Convert.ToDateTime(startDate + " 00:00:00") &&
                                                        ps.ModifiedDate <= Convert.ToDateTime(endDate + " 23:59:59")
                                                        )
                                                );
                    } else {
                        persons = persons.Where(ps => (
                                                        ps.CreatedDate >= Convert.ToDateTime(startDate + " 00:00:00")
                                                        ||
                                                        ps.ModifiedDate >= Convert.ToDateTime(startDate + " 00:00:00")
                                                      )
                                                );
                    }
                } else if (!String.IsNullOrEmpty(endDate)) {
                    persons = persons.Where(ps => (
                                                    ps.CreatedDate >= Convert.ToDateTime(endDate + " 23:59:59")
                                                    ||
                                                    ps.ModifiedDate >= Convert.ToDateTime(endDate + " 23:59:59")
                                                  )
                                            );
                }
                break;
            case "c":
                if (!String.IsNullOrEmpty(startDate)) {
                    if (!String.IsNullOrEmpty(endDate)) {
                        persons = persons.Where(ps => (
                                                        ps.CreatedDate >= Convert.ToDateTime(startDate + " 00:00:00") &&
                                                        ps.CreatedDate <= Convert.ToDateTime(endDate + " 23:59:59")
                                                        )
                                                );
                    } else {
                        persons = persons.Where(ps => (
                                                        ps.CreatedDate >= Convert.ToDateTime(startDate + " 00:00:00")
                                                      )
                                                );
                    }
                } else if (!String.IsNullOrEmpty(endDate)) {
                    persons = persons.Where(ps <= (
                                                    ps.CreatedDate >= Convert.ToDateTime(endDate + " 23:59:59")
                                                  )
                                            );
                }
                break;
            case "m":
                if (!String.IsNullOrEmpty(startDate)) {
                    if (!String.IsNullOrEmpty(endDate)) {
                        persons = persons.Where(ps => (
                                                        ps.ModifiedDate >= Convert.ToDateTime(startDate + " 00:00:00") &&
                                                        ps.ModifiedDate <= Convert.ToDateTime(endDate + " 23:59:59")
                                                        )
                                                );
                    } else {
                        persons = persons.Where(ps => (
                                                        ps.ModifiedDate >= Convert.ToDateTime(startDate + " 00:00:00")
                                                      )
                                                );
                    }
                } else if (!String.IsNullOrEmpty(endDate)) {
                    persons = persons.Where(ps <= (
                                                    ps.ModifiedDate >= Convert.ToDateTime(endDate + " 23:59:59")
                                                  )
                                            );
                }
                break;
        }
这似乎是错误的。这是规定的方法,还是有人能建议一种更有效的方法,最好不要使用“00:00:00”/“23:59:59”


感谢

您可以通过将测试推送到SQL来简化-条件运算符在时将转换为SQL
大小写,因为它们不是简单的常量。注意,我假设您的代码样本中有
endDate
测试。还有很多重复的子表达式,我将它们合并为变量。由于您使用的是EF Core,因此没有比您使用的更好的方法来处理仅日期比较。在EF中,您可以使用DbFunction,但它仍然不如将日期转换为适当的日期+时间以便使用索引

var hasStartDate = !String.IsNullOrEmpty(startDate);
var dtStartDate = hasStartDate ? Convert.ToDateTime(startDate + " 00:00:00") : DateTime.MinValue;

var hasEndDate = !String.IsNullOrEmpty(endDate);
var dtEndDate = hasEndDate ? Convert.ToDateTime(endDate + " 23:59:59") : DateTime.MinValue;

var chkCreatedDate = (radCreatedModifiedBoth == "b" || radCreatedModifiedBoth == "c");
var chkModifiedDate = (radCreatedModifiedBoth == "b" || radCreatedModifiedBoth == "m");

persons = persons.Where(ps => (chkCreatedDate ? (hasStartDate ? ps.CreatedDate >= dtStartDate : true) && (hasEndDate ? ps.CreatedDate <= dtEndDate : true) : true)
                               ||
                              (chkModifiedDate ? (hasEndDate ? ps.ModifiedDate >= dtStartDate : true) && (hasEndDate ? ps.ModifiedDate <= dtEndDate : true) : true)
                        );
var hasStartDate=!String.IsNullOrEmpty(startDate);
var dtStartDate=hasStartDate?Convert.ToDateTime(startDate+“00:00:00”):DateTime.MinValue;
var hasEndDate=!String.IsNullOrEmpty(endDate);
var dtEndDate=hasEndDate?Convert.ToDateTime(endDate+“23:59:59”):DateTime.MinValue;
var chkCreatedDate=(radCreatedModifiedBoth=“b”| | radCreatedModifiedBoth=“c”);
var chkModifiedDate=(radCreatedModifiedBoth=“b”| | radCreatedModifiedBoth=“m”);

persons=persons.其中(ps=>(chkCreatedDate?(hasStartDate?ps.CreatedDate>=dtStartDate:true)和(hasEndDate?ps.CreatedDate=dtStartDate:true)和(hasEndDate?ps.ModifiedDate尝试在stackechange code review中发布此内容。我认为你不会在上面找到你想要的内容。当你只有一个
endDate
向后时,你的案例是否适用?如果你有
startDate
endDate
你测试介于两者之间,但如果你只有
startDate
或只是
endDate)e> ,你们两个都测试得更好吗?谢谢,这将有助于我快速地复制和粘贴;)这似乎更明智,我投了更高的票,但我太穷了,不能让它出现:)谢谢
var hasStartDate = !String.IsNullOrEmpty(startDate);
var dtStartDate = hasStartDate ? Convert.ToDateTime(startDate + " 00:00:00") : DateTime.MinValue;

var hasEndDate = !String.IsNullOrEmpty(endDate);
var dtEndDate = hasEndDate ? Convert.ToDateTime(endDate + " 23:59:59") : DateTime.MinValue;

var chkCreatedDate = (radCreatedModifiedBoth == "b" || radCreatedModifiedBoth == "c");
var chkModifiedDate = (radCreatedModifiedBoth == "b" || radCreatedModifiedBoth == "m");

persons = persons.Where(ps => (chkCreatedDate ? (hasStartDate ? ps.CreatedDate >= dtStartDate : true) && (hasEndDate ? ps.CreatedDate <= dtEndDate : true) : true)
                               ||
                              (chkModifiedDate ? (hasEndDate ? ps.ModifiedDate >= dtStartDate : true) && (hasEndDate ? ps.ModifiedDate <= dtEndDate : true) : true)
                        );