2012-02-17 22 views
0

我有一個Lambda表達式,它根據傳入的ID過濾客戶列表。這很好,但是我想從CreationDate字段中刪除時間戳,當我返回記錄。無論如何要在Lambda表達式中做到這一點?更新/修剪Lambda中的值表達式

所以這是我的Lambda表達式返回我的客戶記錄:

customers = customers.Where(c => c.Business_Type == businessType); 

不過,我想這樣做如下:

customers = customers.Where(c => c.Business_Type == businessType, c.CreationDate=c.CreationDate.Value.ToShortDateString()); 
+2

要回答你的問題,是的,你可以。在以下方法中轉換表達式:customers = customers.Where(c => {bool r = c.Business_Type == businessType; c.CreationDate = c.CreationDate.Value.ToShortDateString(); return r});不是建議... – Polity 2012-02-17 10:02:31

回答

4

LINQ並不意味着執行的突變序列元素。只要採取的Where返回值,並使用foreach進行突變,這是慣用的方式來處理這個問題:

var customers = customers.Where(c => c.Business_Type == businessType).ToArray(); 
foreach(var c in customers) 
{ 
    c.CreationDate = c.CreationDate.Value.ToShortDateString(); 
} 
+0

非常感謝您的回覆 – user1131661 2012-02-17 10:53:53