2017-02-21 71 views
-3

現在我知道,當x.transaction爲null時,它將跳過。但是,如何在.Where()擴展方法中設置一個值?正如你可以看到第一行有一個條件,如果id不是null,那麼將該值設置爲零。但是如何在.Where()擴展中如何設置一個值,如果x.TransactionPrice爲空?在第三行。如何在.Where()擴展中設置零值(如果值爲空)

就在此代碼示例:

var Rs12 = id != null 
       ? _context.DwPropertyDetails 
        .Where(x => x.LandId == id && x.TransactionPrice != null) 
        .OrderByDescending(x => x.TransactionPrice) 
        .AsEnumerable() 
        .Select(
         (x, index) => 
          new 
          { 
           TRANSACTION_PRICE = x.TransactionPrice ?? (long?)0, 
           ACTUAL_SIZE = x.ActualSize, 
           rank = index + 1 
          }) 
        .Where(x => x.rank == 1).Select(x => new 
        { 
         TRAN_S = x.TRANSACTION_PRICE ?? (long?)0 
        }) 
        .SelectMany(
         TranS => 
          _context.DwPropertyDetails.Where(
            x => x.LandId == id && x.TransactionPrice != null) 
           .OrderByDescending(
            x => 
             (x.TransactionPrice ?? 0)/
             (x.ActualSize == null || x.ActualSize == 0 ? 1 : x.ActualSize) ?? 1) 
           .AsEnumerable() 
           .Select(
            (x, index) => 
             new 
             { 
              PER_FT_S = 
              (x.TransactionPrice ?? 0)/
              (x.ActualSize == null || x.ActualSize == 0 ? 1 : x.ActualSize) ?? 1, 
              rank = index + 1 
             }) 
           .Where(x => x.rank == 1).Select(x => new 
           { 
            x.PER_FT_S 
           }), (TranS, PerFtS) => new 
           { 
            TranS.TRAN_S, 
            PerFtS.PER_FT_S 
           }) 
       : _context.DwPropertyDetails.Select(x => new 
       { 
        TRAN_S = (long?)0, 
        PER_FT_S = (long)0 
       }); 

回答

0

.Where()延伸接受一個謂語,所以你可以使用statement lambdas代替expression lambdas和設置的值。

class A 
    { 

     public int Id { get; set; } 
     public string Name { get; set; } 
     public string Address { get; set; } 
     public string AddressPresent { get; set; } 
    } 

這裏是這臺內拉制

static void WhereDemo() 
     { 
      List<A> aList = new List<A>{ 

        new A{ Id=1,Name="Hi",Address="Toronto" }, 
        new A{ Id=2,Name="Him",Address="NY" }, 
        new A{ Id=3,Name="His" }, 
        new A{ Id=4,Name="quad",Address="MS" }, 
      }; 

      var queryOutput= aList.Where(x => { 
       if (x.Address != null) 
       { 
        x.AddressPresent = "Present"; 
        return true; // selects the element 
       } 
       else 
       { 
        x.AddressPresent = "Absent"; 
        return false; // does not select the element 
       } 

      }); 

      foreach (var element in aList) 
      { 

       Console.WriteLine("{0}\t{1}\t{2}\t{3}", element.Id, element.Name, element.Address, element.AddressPresent); 
      } 
      Console.WriteLine("-----------------------"); 
      foreach (var item in queryOutput) 
      { 
       Console.WriteLine("{0}\t{1}\t{2}\t{3}", item.Id, item.Name, item.Address, item.AddressPresent); 

      } 
     } 

輸出是

1  Hi  Toronto 
2  Him  NY 
3  His 
4  quad MS 
----------------------- 
1  Hi  Toronto Present 
2  Him  NY  Present 
4  quad MS  Present 

值可以看到,它僅影響查詢輸出,雖然不是原來的列表元素的方法。

+0

謝謝。但不起作用。 –

+0

你可以給一些測試用例嗎?沒有測試用例很難得到你想要的東西 –

+0

怎麼樣?如何檢查視圖中的模型是否爲null,然後將值設置爲屬性。怎麼做? –

相關問題