简体   繁体   English

按日期时间过滤 LINQ 查询导致 System.NotSupportedException 错误

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

I have a database table containing a list of Secondary School Surveys.我有一个包含中学调查列表的数据库表。 I have a controller which displays a list of the entries in the DB table.我有一个控制器,它显示数据库表中的条目列表。 I am trying to allow a user to search for specific survey entries on "dd-MM-yyyy" date.我试图允许用户在“dd-MM-yyyy”日期搜索特定的调查条目。

I have tried the following, although the return View(surveys.ToPagedList(pagerNumber, pageSize)) throws the following error when I search for a date:我尝试了以下操作,尽管当我搜索日期时返回 View(surveys.ToPagedList(pagerNumber, pageSize)) 会引发以下错误:

System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.' System.NotSupportedException: 'LINQ to Entities 无法识别方法 'System.String ToString(System.String)' 方法,并且此方法无法转换为存储表达式。

My Index Controller:我的索引控制器:

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

I could possibly use the SqlFunctions class ie SqlFunctions.StringConvert but how would I then convert DateTime to the correct format (dd-MM-yyyy)?我可能会使用 SqlFunctions 类,即 SqlFunctions.StringConvert 但是我如何将 DateTime 转换为正确的格式 (dd-MM-yyyy)?

I also tried converting the search string to DateTime although kept getting a Sys.FormatException: "The string was not recognized as a valid DateTime".我也尝试将搜索字符串转换为 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);
        }

If I understand the question you could use DateTime.TryParse如果我理解你可以使用DateTime.TryParse的问题

Converts the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded.将日期和时间的指定字符串表示形式转换为其等效的 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))
}

Note : There are other variations of how to do this, you could have it all in a single query, though I think this is explicit and is simple to understand注意:还有其他方法可以做到这一点,您可以在一个查询中全部完成,尽管我认为这是明确的并且易于理解

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 linq“System.NotSupportedException”出错 - There is an error with linq “System.NotSupportedException” System.NotSupportedException使用DateTime吗? 在Linq to实体 - System.NotSupportedException using DateTime? in Linq to Entities LINQ嵌套查询问题-System.NotSupportedException - LINQ nested query issue - System.NotSupportedException LINQ错误:GroupBy选择上出现System.NotSupportedException - LINQ error: System.NotSupportedException on GroupBy Selection LINQ GroupBy子句System.NotSupportedException错误 - LINQ GroupBy Clause System.NotSupportedException error ASP.NET MVC System.NotSupportedException LINQ到实体查询 - ASP.NET MVC System.NotSupportedException LINQ to Entities query System.NotSupportedException:当我运行此 Linq 查询时 - System.NotSupportedException : When I run this Linq query DefaultIfEmpty()引起“ System.NotSupportedException:LINQ to Entities无法识别方法'System.Collections.Generic.IEnumerable'” - DefaultIfEmpty() Causing “System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable'” System.NotSupportedException:如何简化NHibernate查询? - System.NotSupportedException: How to simplify the NHibernate Query? 为什么在 datagridview 的列中按日期过滤时会出现错误? 'System.NotSupportedException' DataGridViewAutoFilter.dll - Why do I get an error when filtering by date in a column of the datagridview? 'System.NotSupportedException' DataGridViewAutoFilter.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM