2017-05-23 13 views
1

회원 테이블에 Birthdate라는 null 가능 DateTime 필드가 있습니다. 다음 며칠 동안 생일을 맞은 회원을 입회시켜야합니다. 나는 몇 가지 방법을 시도했지만 그들 중 누구도 일하지 않았습니다.NHibernate n 일 후 생일을 가진 회원을 얻는 방법

DateTime startDate = DateTime.Now; 
DateTime endDate = DateTime.Now.AddDays(n); 

GetAll().Where(
    x => x.BirthDate != null 
    && new DateTime(startDate.Year, x.BirthDate.Value.Month, x.BirthDate.Value.Day, 1, 1, 1) >= startDate 
    && new DateTime(endDate.Year, x.BirthDate.Value.Month, x.BirthDate.Value.Day, 1, 1, 1) <= endDate 
    ); 

1

2

GetAll().OrderByDescending(m => m.CreateDate) 
    .Where(x => x.BirthDate.HasValue 
     && (endDate - x.BirthDate.Value).Days <= n) 

는 인식 오류가 발생 날짜 시간 생성자 x.BirthDate을 통과 할 수 없다.

이 작업을 수행하는 간단한 방법을 알고 계십니까?

+0

'GetAll()'이 (가) 필터링을 시작하기 전에 반환하는 것을 먼저 들여다 보셨습니까? – yoger

답변

0

DateTimeDateTimeOffset에 많은 속성을 지원합니다. 목록 here을 볼 수 있습니다. reference documentation도 언젠가 전용 Linq 섹션을 갖게 될 것입니다. (이미 최선을 다하고, 아직 해제되지 않습니다.)

그래서, GetAllIQueryable를 산출하여 추측, 당신은 방법으로 쿼리 작성할 수 있습니다

var startDate = DateTime.Now; 
var endDate = DateTime.Now.AddDays(n); 
if (startDate.Year == endDate.Year) 
{ 
    // Simple case, just compare months and days. 
    GetAll().Where(
     x => x.BirthDate.Value.Month >= startDate.Month && 
      x.BirthDate.Value.Day >= startDate.Day && 
      x.BirthDate.Value.Month <= endDate.Month && 
      x.BirthDate.Value.Day <= endDate.Day); 
} 
else 
{ 
    // Range spanning two distinct years, so matching dates 
    // are either lower months and days than endDate, OR 
    // greater months and days than startDate. (Cannot have both.) 
    GetAll().Where(
     x => x.BirthDate.Value.Month >= startDate.Month && 
      x.BirthDate.Value.Day >= startDate.Day || 
      x.BirthDate.Value.Month <= endDate.Month && 
      x.BirthDate.Value.Day <= endDate.Day); 

} 

내가 HasValue에 체크를 제거한를 : 쿼리가 번역되어있는 경우 이 경우에는 SQL에 SQL이 필요하지 않습니다.

도 역시 과 작동해야하며, 동일한 DateTime 속성을 지원한다고 생각합니다.