我有一個關於編寫linq查詢的常見做法的問題。其實我和我的同事吵了一架。Linq編碼風格:內聯如果
我們有一個布爾變量,如果它是真的 - 需要額外的檢查。如果它是錯誤的,則不應該有這樣的檢查。
有實現它在LINQ 2路:
bool onlyForReturningCustomers;
.....
return context.Products.Where(product => product.StartTime >= fromDate
&& product.StartTime < toDate
&& (onlyForReturningCustomers ? product.IsReturningClient : true));
與第二:
bool onlyForReturningCustomers;
.....
var q = context.Products.Where(product => product.StartTime >= fromDate && product.StartTime < toDate);
if (onlyForReturningCustomers) {
q = q.Where(product => product.IsReturningClient);
}
return q;
第一個呈現在SQL case
聲明,當onlyForReturningCustomers=false
像1=1
出現的語句,但該代碼被讀取更容易。
第二個不容易閱讀,但它在sql中顯示清晰的語句沒有任何垃圾。
你會使用哪一個?
看起來你已經有了可用的工具/知識來回答這個問題。 – 2012-04-05 09:29:08