2013-07-01 3 views
0

this answer에서 언급 한 일부 논리를 사용하여이 코드를 사용하여 몇 가지 테이블에서 외부 조인을 수행합니다. 세 조인 중 하나가 MyField 값을 산출한다는 것을 알고 있습니다. 그러나 select 문에서 생성자를 호출하고 null이 아닌 MyField 값만 사용하려고합니다.nullable 필드에서 Linq 외부 조인

public static MyResult GetMyResult(string MyString) 
{ 
    var result = (from w in context.TABLE_ONEs 
     from a in context.TABLE_TWOs.Where(x => x.field1 == w.field1).DefaultIfEmpty() 
     from l in context.TABLE_THREEs.Where(y => y.field1 == w.field1).DefaultIfEmpty() 
     from n in context.TABLE_FOURs.Where(z => z.field1 == w.field1).DefaultIfEmpty() 
     select new MyResult(w.someField, 
      w.someOtherField, 
      (a.date ?? l.date ?? n.date)) 
     ).First(); 

     return result; 
    } 

그러나 컴파일 오류가 발생합니다. "연산자 '??' 'System.DateTime'및 'System.DateTime'형식의 피연산자에는 적용 할 수 없습니다. 이 nullable 데이터 형식 (예 : 문자열 있지만이 "날짜"필드에 잘 작동합니다.

데이터베이스 열을 null 가능 DateTime으로 변경하는 것 외에이를 해결하는 방법을 알고 있습니까?

+0

** DateTime ** 필드의 기본값은 ** DateTime.MinValue **가 데이터베이스에 설정되어 있지 않은 경우입니까? 이 경우에는 다음과 같은 추악한 무언가가 작동합니다. ** (a.date! = DateTime.MinValue? a.date : (l.date! = DateTime.MinValue? l.date : n.date)) ** –

+0

@ThomasC .G.deVilhena :이 솔루션을 시도하고 System.Reflection.TargetOfInvocationException을 얻었습니다. –

답변

1

다음은 매우 깨끗한 것은 아니지만 작동해야합니다.

date = (DateTime?) (a != null) ? a.date : (l != null) ? l.date : (n != null) ? n.date : null 

null 가능 DateTime 대신 MinDate를 사용하므로 값 유형으로 유지됩니다.