2016-10-18 4 views
1

(まだC#6.0)ネストされた条件演算子2013 VSを使用して

I持って働き、次のLINQ:

var radData = (from stop in dbContext.stop_details 
       join del in dbContext.stop_event on stop.id equals del.stop_id into Inners 
       from sd in Inners.DefaultIfEmpty() 
       where stop.ship_date == startDate && stop.cust_ref_5_terminalID == "HEND" 
       select new 
       { 
        shipDate = stop.ship_date, 
        custRef = stop.cust_ref_5_terminalID, 
        name = stop.customer.customer_name, 
        ontime = (int?)sd.ontime_performance, 
        OTP = ((int?)sd.ontime_performance) < 1 ? "Ontime" : "Late" 
       }).ToList(); 

しかしOTPニーズの値を次のように依存しますontime_performance

  • ヌル -
  • < 1 "オンタイム" を "開く"
  • 1 "一日遅く、"
  • 2 "二日遅れ"
  • 「> 2 "三日以上遅れて"

はこの巣に方法はありますか?私がこれまでに試したことはありません。

ありがとうございます。次のように

答えて

1

あなたはチェーン多くの?:ことができます。

var radData = (from stop in dbContext.stop_details 
       join del in dbContext.stop_event on stop.id equals del.stop_id into Inners 
       from sd in Inners.DefaultIfEmpty() 
       where stop.ship_date == startDate && 
        stop.cust_ref_5_terminalID == "HEND" 

       let value = ((int?)sd.ontime_performance) 
       select new 
       { 
        shipDate = stop.ship_date, 
        custRef = stop.cust_ref_5_terminalID, 
        name = stop.customer.customer_name, 
        ontime = (int?)sd.ontime_performance, 
        OTP = value == null ? "Open" : 
         value < 1 ? "On time" : 
         value == 1 ? "One Day Late" : 
         value == 2 ? "Two Days Late" : "Three or more days late" 
       }).ToList(); 

をあなたはそれを毎回キャストする必要はありませんので、また、あなたは変数にフィールドを格納することができます:let value = ((int?)sd.ontime_performance)

をBTW - フィールドをint?の場合はvalue < 1value == 0に変更することができます。他の条件と一貫しており、あまり混乱はありません。

+0

パーフェクト - 値の部分もありがとうございます。負の値も返されているので、<1は維持する必要があります。素晴らしい答え。 –

+0

@MostlyLucid - あなたは歓迎です:) –