지정된 형식 멤버 'Date'는 LINQ to Entities Exception에서 지원되지 않습니다.
다음 문을 구현하는 동안 예외가 발생했습니다.
DateTime result;
if (!DateTime.TryParse(rule.data, out result))
return jobdescriptions;
if (result < new DateTime(1754, 1, 1)) // sql can't handle dates before 1-1-1753
return jobdescriptions;
return jobdescriptions.Where(j => j.JobDeadline.Date == Convert.ToDateTime(rule.data).Date );
예외
The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
예외의 의미는 알고 있지만 제거하는 방법을 모르겠습니다. 도움이 필요하세요?
LINQ to Entities는 동등한 SQL이 없기 때문에 대부분의 .NET Date 메서드 (사용한 캐스팅 포함)를 SQL로 변환 할 수 없습니다.
해결책은 LINQ 문 외부에서 Date 메서드를 사용한 다음 값을 전달하는 것입니다. Convert.ToDateTime (rule.data) .Date가 오류를 일으키는 것처럼 보입니다.
DateTime 속성에 대한 호출 날짜도 SQL로 변환 할 수 없으므로 해결 방법은 정수일 뿐이므로 LINQ로 변환 할 수있는 .Year .Month 및 .Day 속성을 비교하는 것입니다.
var ruleDate = Convert.ToDateTime(rule.data).Date;
return jobdescriptions.Where(j => j.Deadline.Year == ruleDate.Year
&& j.Deadline.Month == ruleDate.Month
&& j.Deadline.Day == ruleDate.Day);
EntityFunctions 의 TruncateTime 메서드를 사용 하여 속성을 SQL로 올바르게 변환 할 수 있습니다 .Date
using System.Data.Objects; // you need this namespace for EntityFunctions
// ...
DateTime ruleData = Convert.ToDateTime(rule.data).Date;
return jobdescriptions
.Where(j => EntityFunctions.TruncateTime(j.JobDeadline) == ruleData);
업데이트 : EntityFunctions
EF6에서 더 이상 사용되지 않음, 사용DbFunctions.TruncateTime
EF6의 경우 대신 DbFunctions.TruncateTime (mydate)을 사용하십시오.
ef6의 "EntityFunctions.TruncateTime"또는 "DbFunctions.TruncateTime"이 작동하지만 빅 데이터에서 일부 성능 문제가 있습니다.
가장 좋은 방법은 다음과 같이 행동하는 것입니다.
DateTime ruleDate = Convert.ToDateTime(rule.data);
DateTime startDate = SearchDate.Date;
DateTime endDate = SearchDate.Date.AddDay(1);
return jobdescriptions.Where(j.Deadline >= startDate
&& j.Deadline < endDate );
날짜의 일부를 사용하는 것보다 낫습니다. 큰 데이터에서 쿼리가 더 빠르게 실행되기 때문입니다.
사용해보십시오
return jobdescriptions.AsEnumerable()
.Where(j => j.JobDeadline.Date == Convert.ToDateTime(rule.data).Date );
AsEnumerable()
쿼리 컨텍스트를 LINQ에서 Entities로 LINQ to Objects로 전환하므로 조건이 SQL로 변환되지 않습니다.
의미하는 바는 LINQ to SQL이 Date
속성을 SQL 식으로 변환하는 방법을 모른다는 것 입니다. 이는 구조 의 Date
속성이 DateTime
SQL에서 아날로그 가 없기 때문 입니다.
그것은 나를 위해 일했습니다.
DateTime dt = DateTime.Now.Date;
var ord = db.Orders.Where
(p => p.UserID == User && p.ValidityExpiry <= dt);
출처 : Asp.net 포럼
동일한 문제가 있지만 DateTime-Ranges로 작업합니다. 내 솔루션은 시작 시간 (모든 날짜 포함)을 00:00:00으로, 종료 시간을 23:59:59로 조작하는 것이므로 더 이상 DateTime을 날짜로 변환하지 않고 DateTime으로 유지합니다.
DateTime이 하나만있는 경우 시작 시간 (모든 날짜 포함)을 00:00:00으로 설정하고 종료 시간을 23:59:59로 설정할 수도 있습니다. 그런 다음 마치 시간 범위 인 것처럼 검색합니다.
var from = this.setStartTime(yourDateTime);
var to = this.setEndTime(yourDateTime);
yourFilter = yourFilter.And(f => f.YourDateTime.Value >= from && f.YourDateTime.Value <= to);
DateTime-Range로도 할 수 있습니다.
var from = this.setStartTime(yourStartDateTime);
var to = this.setEndTime(yourEndDateTime);
yourFilter = yourFilter.And(f => f.YourDateTime.Value >= from && f.YourDateTime.Value <= to);
다음과 같이 Enum을 얻을 수 있습니다.
DateTime todayDate = DateTime.Now.Date; var check = db.tableName.AsEnumerable().Select(x => new
{
Date = x.TodayDate.Date
}).Where(x => x.Date == todayDate).FirstOrDefault();
'Programming' 카테고리의 다른 글
android : 피카소로 원형 이미지 만들기 (0) | 2020.08.19 |
---|---|
vi 편집기에서 문자열 검색 및 개수 가져 오기 (0) | 2020.08.19 |
TableViewController, iOS에서 여분의 빈 셀을 제거하는 방법-Swift (0) | 2020.08.19 |
Android 5.0의 Datepicker 대화 상자 색상 변경 (0) | 2020.08.19 |
어떤 사용자 PHP가 실행 중인지 확인하는 방법은 무엇입니까? (0) | 2020.08.19 |