2017-01-23 8 views
-1

여기서 Any/null에 대한 검사를 어떻게 제거합니까?IEnumerable 결과 집합에서 null 가능성을 제거하는 방법

public List<Incident> GetByServiceActivity(Guid serviceActivityGuid) 
    { 
     var result = from s in this._xrmServiceContext.ServiceAppointmentSet 
         join i in this._xrmServiceContext.IncidentSet on s.RegardingObjectId.Id equals i.Id 
         select i; 

     return result.Any() ? result.Distinct().ToList() : new List<Incident>(); 
    } 

I는 다음과 같이 단순히 1 식을 반환 할 수 있도록하고 싶습니다 :

return from s in this._xrmServiceContext.ServiceAppointmentSet 
          join i in this._xrmServiceContext.IncidentSet on s.RegardingObjectId.Id equals i.Id 
          select i; 

어떻게 우리가 오히려 (.ANY를 확인하는 것보다, 암시 적으로 빈을 반환 할 수 있습니다)? IEnumerable<T>를 반환 LINQ 방법

+1

결과 집합이 없으면'Select'는 항상 빈 Enumerable을 반환합니다. 그래서 xrmServiceContext.ServiceAppointmentSet에있는의 xrmServiceContext.IncidentSet에서 join i를 i.Id와 같게select i) .ToList()'또는 반환 할 메소드를'public IEnumerable GetByServiceActivity (Guid serviceActivityGuid) ' – dynamicallyCRM

답변

5

없음 null 값을 리턴하지 것이다. 그들은 항상 null이 아닌 IEnumerable 또는 IQueryable을 반환합니다. 그것이 나타내는 시퀀스는 비어 일 수 있지만 결코 null이되지는 않습니다.

빈 상자를 처리하는 경우 처리 할 항목이 없습니다. Distinct을 비어있는 시퀀스/쿼리에서 비효율적 인 방식으로 효과적으로 호출 할 수 있으며 비어 있거나 비어 있지 않은 시퀀스에서 목록을 만들 수 있습니다 (ToList). 아무것도 확인하지 않아도됩니다.

+2

물론 단일 인스턴스 또는 null/zero를 반환하는'FirstOrDefault '와 같은 메소드는 제외합니다. –

+0

이 적? 널이 될 수 VAR 결과 = (행 번호 _cmContext.SrvLocationWarmLine 에서 어디 String.Compare (number.CurrentWarmLine, startingRange, StringComparison.Ordinal)> = 0 && String.Compare (number.CurrentWarmLine, endingRange, StringComparison.Ordinal) <= 0 orderby number.CurrentWarmLine 내림차순 select new {Number = number.CurrentWarmLine}. FirstOrDefault(); –

+0

@MeggieLuski 예, 시퀀스를 반환하지 않으므로 시퀀스에서 단일 값을 반환합니다. – Servy