2011-10-20 10 views
2

私は複雑で(むしろハッキーな)動的SQLクエリをLINQクエリに変換しようとしています。LINQの結果がなくなりました

私はこれまで、次のLINQクエリを持っている:

var results = (
    from c in Customers 
    from d in MonthCalendar 
     join f in Facilities on c.CustomerCode equals f.CustomerCode 
     join p in ProjectedCashFlows on f.FacilityId equals p.FacilityId into pj 
       from p in pj.DefaultIfEmpty()      
     where d.ExpectedYear == currentYear 
       && f.FacilityStatusId == 1 
       && (p.ExpectedYear == null || d.ExpectedYear == p.ExpectedYear) 
       && (p.ExpectedMonth == null || d.ExpectedMonth == p.ExpectedMonth) 
       && c.PrimaryArmId == userId 
       && (p.ProjectedCashFlowStatusId == null || p.ProjectedCashFlowStatusId != 4) 
     select new 
      { 
       CustomerCode = c.CustomerCode, 
       CustomerName = c.CustomerName, 
       FacilityId = f.FacilityId, 
       FacilityDescription = f.FacilityProductDescription, 
       FacilityCurrency = f.FacilityCurrencyId, 
       FacilityLimit = f.Limit, 
       ExpectedYear = d.ExpectedYear, 
       ExpectedMonth = d.ExpectedMonth, 
       ExpectedAmount = p == null ? 0 : (double)p.ExpectedAmount 

      } 
      ); 

私はFacilitiesテーブルと1対多の関係を持っていCustomerテーブルから詳細を取得しようとしています。私はその後、私が午前問題は、クエリに関係なく、任意の値がProjectedCashFlowsテーブルに存在するかどうかのすべてCustomerFacilites情報を返すべきであるということですProjectedCashFlows

に配置されているすべての詳細を取得しようとしています。

残念ながら、このクエリは実行していません。ProjectedCashFlowsテーブルにファシリティが存在する場合、情報はCustomerFacilitiesという情報のみを返します。

私はMonthCalenderテーブルを使用して、その年の各月を一覧表示しています。

関連するテーブル情報は、次のとおりです。

お客様

  • CustomerCode
  • CustomerNameの
  • PrimaryArmId

施設

  • CustomerCode
  • FacilityId
  • FacilityCurrencyId
  • FaciliyLimit
  • FacilityDescription

ProjectedCashFlows

  • CustomerCode
  • FacilityId
  • ExpectedYear
  • ExpectedMonth
  • ExpectedAmount
  • ProjectedCashFlowStatusId

MonthsCalendar

  • ExpectedMonth
  • ExpectedYear

私はしかしFacilities表に4行を持っている顧客を持っている例として、それらが表示されていないので、これらの施設の2はProjectedCashFlowsテーブルには表示されません。エントリがProjectedCashFlowsに存在しない場合

それは、CalendarMonthsテーブルからExpectedMonth & ExpectedYearを取るExpectedAmountに0を返し、FacilitiesテーブルからFacilityIdを使用する必要があります。

あなたはおそらく私はLINQを使い始めたばかりです。

誰でも正しい方向に私を突きつけられますか?

答えて

2

あなたのクエリは、それが非nullであると仮定しpを使用しています。

where d.ExpectedYear == currentYear 
     && f.FacilityStatusId == 1 
     && (p.ExpectedYear == null || d.ExpectedYear == p.ExpectedYear) 
     // etc 

しかし、あなたは何ProjectedCashFlowsがない場合、論理的に単一のヌル値を持つシーケンスを作成しますDefaultIfEmpty()を使用しました。

where d.ExpectedYear == currentYear 
     && f.FacilityStatusId == 1 
     && (p == null || 
      ((p.ExpectedYear == null || d.ExpectedYear == p.ExpectedYear) 
      // etc 
     )) 

だから基本的に、あなたのような何かを必要とします

関連する問題