在LINQ

时间:2017-07-26 09:24:48

标签: linq entity-framework-core

我试图通过LINQ和Entity Framework CORE对表进行搜索。我有两个文本框startdate和enddate以及一个3个选项的单选按钮,创建,修改和两者。

这是我根据Google搜索和教程制作的代码

       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;
        }

此代码有效,但效率非常低,更不用说将开始和结束时间作为字符串添加到日期

startDate + " 00:00:00"
endDate + " 23:59:59"

似乎错了。这是规定的方法还是任何人都可以建议一种更有效的方法,最好是摆脱&#34; 00:00:00&#34 /&#34; 23:59:59&#34;

由于

1 个答案:

答案 0 :(得分:1)

您可以通过将测试推送到SQL来简化 - 条件运算符将被转换为SQL CASE WHEN,因为它们不是简单的常量。请注意,我假设您在代码示例中向后进行了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)
                        );