如何在nopcommerce c#中的linq quary中将日期时间转换为日期

问题描述 投票:0回答:1
 query = (from s in _studentRepository.Table
     //from a in _attendanceRepository.Table.Where(j => j.StudentId == s.Id && (j.Date == date.Date)).DefaultIfEmpty()
     from a in _attendanceRepository.Table.Where(j => j.StudentId == s.Id).DefaultIfEmpty()
     where s.ClassSectionId == searchClassSectionId && (a.Date == Convert.ToString(date, "DD/MM/YYYY") || a.Date == null)
     select new
     {
         AttendanceTypeId = (a != null ? a.AttendanceTypeId : 0),
             Date = (a != null ? a.Date : default(DateTime)),
             Id = (a != null ? a.Id : 0),
             Student = s
     }).ToList().Select(x => new Attendance()
     {
         Id = x.Id,
         AttendanceTypeId = x.AttendanceTypeId,
         Date = x.Date,
         Student = x.Student
     }).ToList();
c# function date nopcommerce
1个回答
1
投票

您的问题是格式不属于Linq查询本身。查询与数据有关,与表示无关。发生这种情况是因为LINQ to Entities试图将表达式树转换为SQL查询,并且.ToString(string)无法转换为SQL。然后在进行格式化之前,请确保枚举集合。您可以调用.ToList(),也可以使用范围日期。

我认为以下代码可以为您提供帮助

query = (from s in _studentRepository.Table
                     //from a in _attendanceRepository.Table.Where(j => j.StudentId == s.Id && (j.Date == date.Date)).DefaultIfEmpty()
                 from a in _attendanceRepository.Table.Where(j => j.StudentId == s.Id).DefaultIfEmpty()
                 where s.ClassSectionId == searchClassSectionId && (a.Date >= DateTime.Parse(date.ToString(CultureInfo.InvariantCulture)).Date 
                 && a.Date < DateTime.Parse(date.ToString(CultureInfo.InvariantCulture))
                 .Date.AddDays(1).AddTicks(-1) || a.Date == null)
                 select new
                 {
                     AttendanceTypeId = (a != null ? a.AttendanceTypeId : 0),
                     Date = (a != null ? a.Date : default(DateTime)),
                     Id = (a != null ? a.Id : 0),
                     Student = s
                 }).ToList().Select(x => new Attendance()
                 {
                     Id = x.Id,
                     AttendanceTypeId = x.AttendanceTypeId,
                     Date = x.Date,
                     Student = x.Student
                 }).ToList();
© www.soinside.com 2019 - 2024. All rights reserved.