【问题标题】:Filtering a LINQ query by DateTime causing System.NotSupportedException Error按日期时间过滤 LINQ 查询导致 System.NotSupportedException 错误
【发布时间】:2020-03-24 19:08:44
【问题描述】:

我有一个包含中学调查列表的数据库表。我有一个控制器,它显示数据库表中的条目列表。我试图让用户在“dd-MM-yyyy”日期搜索特定的调查条目。

我尝试了以下方法,尽管在我搜索日期时 return View(surveys.ToPagedList(pagerNumber, pageSize)) 会引发以下错误:

System.NotSupportedException: 'LINQ to Entities 无法识别 method 'System.String ToString(System.String)' 方法,以及这个方法 不能翻译成商店表达式。'

我的索引控制器:

// GET: SecondarySchoolSurvey
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";

            //paging
            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var surveys = from s in db.SecondarySchoolSurveys
                           select s;

            if (!String.IsNullOrEmpty(searchString))
            {
                surveys = db.SecondarySchoolSurveys.Where(s => s.OfficialSchoolName.Contains(searchString) || s.RollNumber.Contains(searchString) || s.CampDate.Value.ToString("dd/MM/yyyy").Contains(searchString));
            }


            switch (sortOrder)
            {
                case "name_desc":
                    surveys = surveys.OrderByDescending(s => s.OfficialSchoolName);
                    break;
                default:
                    surveys = surveys.OrderBy(s => s.OfficialSchoolName);
                    break;
            }



            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(surveys.ToPagedList(pageNumber, pageSize));
        }

我可以使用 SqlFunctions 类,即 SqlFunctions.StringConvert,但我如何将 DateTime 转换为正确的格式 (dd-MM-yyyy)?

我也尝试将搜索字符串转换为 DateTime,尽管不断收到 Sys.FormatException:“字符串未被识别为有效的 DateTime”。

DateTime dt = DateTime.Parse("dd-MM-yyyy", CultureInfo.InvariantCulture);
            if (!String.IsNullOrEmpty(searchString))
            {
                surveys = db.SecondarySchoolSurveys.Where(s => s.OfficialSchoolName.Contains(searchString) || s.RollNumber.Contains(searchString) || s.CampDate == dt);
        }

【问题讨论】:

  • 将日期转换为字符串并使用包含的原因是什么?进一步解释一下
  • 嗨@MichaelRandall,我尝试将搜索字符串转换为DateTime,尽管不断收到Sys.FormatException:“字符串未被识别为有效的DateTime”。您能推荐最佳做法吗?我已经用最后一种方法更新了我的帖子
  • 所以你有一个搜索字符串,它可能包含学校名称,或其他内容,或日期。如果搜索字符串是您想要返回该日期的记录的日期?
  • 正确,这可能吗?

标签: c# asp.net asp.net-mvc asp.net-core


【解决方案1】:

如果我理解您的问题,您可以使用DateTime.TryParse

将日期和时间的指定字符串表示形式转换为其 DateTime 等效并返回一个值,该值指示是否 转换成功。

// Read the documentation on TryParse
var isDate = DateTime.TryParse(searchString, out var searchDate);

// the assumption is if it's a date, 
// then it's not going to satisfy any other search requirements 
if (isDate)
{
    // note, I use .Date as it's likely you don't want to compare time as well
    surveys = db.SecondarySchoolSurveys
                .Where(s => s.CampDate.Date = searchDate.Date);
} 
else if (!String.IsNullOrEmpty(searchString))
{
    surveys = db.SecondarySchoolSurveys
                .Where(s => 
                   s.OfficialSchoolName.Contains(searchString) || 
                   s.RollNumber.Contains(searchString))
}

注意:还有其他方法可以做到这一点,你可以在一个查询中得到所有这些,尽管我认为这是明确的并且是简单易懂

【讨论】:

    猜你喜欢
    • 2018-05-27
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多