2
我有一個L2S存儲庫查詢,我正忙着用一種很好的方式寫。它看起來像...處理鏈式linq-to-sql查詢表達式中的空值
_orderRepository
.GetAllByFilter(o => o.CustomerId == id)
.Select(o =>
new CustomerOrderRecord
(
o.Id,
o.PartNumber,
o.Date
// ... etc, more order details
/* Here I need the last DateTime? the customer placed
an order for this item, which might be null.
So I end up with the following horrible part of
the query */
o.Customer.CustomerOrderRecords
.Where(x => x.PartNumber == o.PartNumber)
.OrderByDescending(x => x.Date).FirstOrDefault()
== null ? null :
o.Customer.CustomerOrderRecords
.Where(x => x.PartNumber == o.PartNumber)
.OrderByDescending(x => x.Date).First().Date;
)).ToList();
所以希望你能看到收到LastOrdered
值當我在寫整個查詢鏈只是做兩次空校驗的問題。
這需要在線(我認爲),因爲GetAllByFilter
返回IQueryable
。
我試圖在select語句中使用一箇中間變量,所以我有點像下面的東西,但我不能得到任何類似的東西來編譯。
是否有可用的語法技巧來解決這個問題?
爲什麼我沒有想到寫這麼認爲的!?很簡單!謝謝 :) – fearofawhackplanet 2011-01-20 13:03:02