2013-04-12 4 views
0

저는 지난 2 일 동안과 철저한 인터넷 검색을 통해 정말 어려움을 겪었습니다. 나는 LINQ와 C#에 대해 매우 익숙하다. 기본적으로, 두 테이블, 모든 하나의 테이블을 조인하려고하지만, 내가 가입하려고하는 열에는 null이 포함됩니다. 그런 다음 다른 테이블의 하위 집합에 해당 열을 결합합니다.LINQ를 사용하여 null 항목과 관련된 문제가 발생했습니다.

내가 참여하고 싶은 것은 합류 한 후 표시되는 첫 번째 테이블의 모든 정보를 표시하는 대신 null이 아닌 열만 표시되는 것입니다.

나는 몇 가지 간단한 puesdo 코드 게시 할 예정입니다, 그래서 이것은 particually 우아한 설명이 있음을 알고 : 그래서

100, 1, 1     TYPE, 1, PC 
101, 1, null    TYPE, 2, Monitor 
102, 1, 2     MODEL, 1, Old PC 
103, 2, null    MODEL, 2, New PC 
104, 2, null    MODEL, 3, Old Monitor 
105, 2, 3     MODEL, 4, New Monitor 

: 닮은 것 테이블에있을 것입니다

tblAsset     tblLookUps 

AssetTag int    Domain String 
Type int     DomainID int 
Model int     Description String 

그래서 정보를 LINQ 쿼리에서 나에게 줄 수있는 내용은 다음과 같습니다.

AssetTag Type TypeDescription Model ModelDescription 
    100  1  PC   1  Old PC 
    101  1  PC   null  null 
    102  1  PC   2  New PC 
    103  2  Monitor  null  null 
    104  2  Monitor  null  null 
    105  2  Monitor  3  Old Monitor 

그러나 순간에 LINQ이 반환

AssetTag Type TypeDescription Model ModelDescription 
    100  1  PC   1  Old PC 
    102  1  PC   2  New PC 
    105  2  Monitor  3  Old Monitor 

을 그러니 확실히 값이 이해 물론, 이는 탈락됩니다 null의 경우 가입하려고 할 때,하지만 난 정말 않도록주의가 null의 경우 그래서 그것을 볼 수 있기를 아주 좋아할 것입니다!

나의 현재 LINQ는 다음과 같습니다

var AllAssets = from assets in dContext.tblAssets 
        join type in dContext.tblLookups.Where(x => x.Domain == "ASTYPE") 
         on assets.Type equals type.DomainID 
        join model in dContext.tblLookups.Where(x => x.Domain == "MODEL") 
         on assets.Model equals model.DomainID 
        select new Asset 
        { 
        AssetTag = assets.AssetTag 
        TypeID = assets.Type 
        TypeDescription = type.Description 
        ModelID = assets.Model 
        ModelDescription = model.Description 
        } 

    return AllAssets; 

내가 .DefaultIfEmpty() 및 여러 다른 것들과 주변 하구 시도했지만 내가의 끝에 도달 한 것처럼 나는 그것을 해결과 느낌을 관리하지 않은 기능, 힌트 또는 포인터가 우수 할 것입니다!

답변

0
var q = from a in tblAssets 
      join l in tblLookUps on a.Model equals l.DomainID 
      into tblAssettblLookUps 
      from l in tblAssettblLookUps.DefaultIfEmpty() 
      select new 
      { 
       AssetTag = a.AssetTag, 
       Type = a.Type,      
       Model = a.Model 
       ModelDescription = l.Description 
      }; 

이렇게 해보십시오.

+0

그건 완벽 했어! 어떻게 처음 그 일을 놓쳤는지 모르겠다. 정말 고마워! –

관련 문제