2016-09-14 1 views
1
var Claim = new[] { new { ClaimId = 10, Desc = "item 10" }, 
    new { ClaimId = 11, Desc = "item 11" }, 
    new { ClaimId = 12, Desc = "item 12" }, 
    new { ClaimId = 13, Desc = "item 13" }}; 
var Insured = new[] { new { ClaimId = 10, IsPolicyHolder = true, IsInsured = true, Forename = "10A Pol TRUE Ins TRUE NoSiblings" }, 
    new { ClaimId = 11, IsPolicyHolder = true, IsInsured = false, Forename = "11A Pol TRUE Ins false NoSiblings" }, 
    new { ClaimId = 12, IsPolicyHolder = false, IsInsured = true, Forename = "12A Pol false Ins TRUE NoSiblings" }, 
    new { ClaimId = 13, IsPolicyHolder = true, IsInsured = false, Forename = "13A Pol TRUE Ins false has sibling" }, 
    new { ClaimId = 13, IsPolicyHolder = false, IsInsured = true, Forename = "13B Pol false Ins TRUE has sibling" }}; 

    Insured.Dump("Insured Table"); 

var leftJoinTry = 
    from in1 in Insured where in1.IsPolicyHolder == true 
    join p in Insured on in1.ClaimId equals p.ClaimId into ps 
    //where ps.IsInsured == true ****ERROR**** 
    from p in ps.DefaultIfEmpty() 
    select new { in1.ClaimId, in1.IsPolicyHolder, in1.IsInsured, in1.Forename, p_Forename = p.Forename}; 
leftJoinTry.Dump("leftJoinTry");  

var initialResults = from c in Claim 
join pa in Insured on c.ClaimId equals pa.ClaimId where pa.IsPolicyHolder == true 
join pb in Insured on pa.ClaimId equals pb.ClaimId where pb.IsInsured == true 
select new { c.ClaimId, pa_Forename = pa.Forename, pb_Forename = pb.Forename, }; 

initialResults.Dump("initialResults"); 

var query = 
    from in1 in Insured where in1.IsPolicyHolder == true 
    join p in Insured on in1.ClaimId equals p.ClaimId into ps 
    //where ps.IsInsured == true 
    from p in ps.DefaultIfEmpty() 
    select new { Category = in1, ProductName = p /*== null ? "(No products)" : p.Forename*/ }; 
query.Dump("query"); 

의 LINQ 패드 위는 다음과 같은 결과가 사용되는 데이터베이스는 ClaimId의 기본 키 (가지고 enter image description here은 LINQ 쿼리 구문 (또는 유창)에 참여

주를 설정 제공 int) 및 IsPolicyHolder (bool)이며 IsPolicyHolder 또는 IsInsured 필드 중 하나가 True 여야한다는 제약 조건이 있습니다.

실제로 원하는 것은 IsPolicyHolder 및 IsInsured가있는 레코드가 모두 True이고 IsPolicyHolder가있는 레코드가 True이고 IsInsured가 False이고 IsPolicyHolder와 동일한 ClaimId가 False이고 IsInsured가 True이면 레코드를 다음과 같이 결합합니다. 청구 13 번. IsPolicyHolder 및 IsInsured 중 하나를 False로 설정하고 다른 하나를 True로 설정 한 다른 ClaimIed 형제가없는 레코드는 포함되어야하며 알 수없는 Forename (아래 이탤릭체)은 알려진 Forename (아래 굵게 표시)으로 설정해야합니다.

enter image description here

답변

0
var firstDate = new DateTime(2016,1,1); 
var secondDate = new DateTime(2016,9,1); 
var company = "Some_Co."; 
var resultCombined = from c in Claims 
join cc in ClaimCovers 
on c.ClaimId equals cc.ClaimId 
join cov in Covers 
on cc.CoverId equals cov.CoverId 
join pa in Insureds.Where(x => x.IsPolicyHolder == true) on c.ClaimId equals pa.ClaimId 
     into pas from pa in pas.DefaultIfEmpty() 
join pb in Insureds.Where(x => x.IsInsured == true) on c.ClaimId equals pb.ClaimId 
     into pbs from pb in pbs.DefaultIfEmpty() 
     join ci in ClaimIncidents 
           on c.ClaimId equals ci.ClaimId 
           where c.DateReported >= firstDate && c.DateReported <= secondDate 
           where c.CompanyID == company 
select new { c.ClaimId, PolicyRefernce = c.PolicyReference ?? "", 
            c.ClaimReference, 
            c.DateOfLoss, 
            c.DateReported, 
            cov.CoverCode, 
            cov.Description, c.Status, 
            ci.NotifiedBy, 
            ci.HowNotified, 
            ci.ClaimTypeCode, 
            ci.CauseCode, 
            ci.AtFault, 
            //pa is the first list where policyHolder = true 
            PolicyHolderName = pa.Forename + " " + pa.Surname, pa_Postcode = pa.Postcode, 
            //pb is the second list where insured = true 
            InsuredName = pb.Forename + " " + pb.Surname, pb_Postcode = pb.Postcode,};  

resultCombined.Dump(); 

비 일치하는 레코드는 ... 빈 문자열 등으로 설정할 수 있습니다 널 (null)을 제공