簡體   English   中英

按日期時間過濾 LINQ 查詢導致 System.NotSupportedException 錯誤

[英]Filtering a LINQ query by DateTime causing System.NotSupportedException Error

我有一個包含中學調查列表的數據庫表。 我有一個控制器,它顯示數據庫表中的條目列表。 我試圖允許用戶在“dd-MM-yyyy”日期搜索特定的調查條目。

我嘗試了以下操作,盡管當我搜索日期時返回 View(surveys.ToPagedList(pagerNumber, pageSize)) 會引發以下錯誤:

System.NotSupportedException: 'LINQ to Entities 無法識別方法 '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);
        }

如果我理解你可以使用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))
}

注意:還有其他方法可以做到這一點,您可以在一個查詢中全部完成,盡管我認為這是明確的並且易於理解

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM