Linq 更新/修剪Lambda where表达式中的值
我有一个Lambda where表达式,它根据传入的ID过滤客户列表。这很好,但是我想在返回记录时从CreationDate字段中删除时间戳。在Lambda表达式中是否有这样做的方法 这是我的Lambda表达式,它返回我的客户记录:Linq 更新/修剪Lambda where表达式中的值,linq,c#-4.0,lambda,where,Linq,C# 4.0,Lambda,Where,我有一个Lambda where表达式,它根据传入的ID过滤客户列表。这很好,但是我想在返回记录时从CreationDate字段中删除时间戳。在Lambda表达式中是否有这样做的方法 这是我的Lambda表达式,它返回我的客户记录: customers = customers.Where(c => c.Business_Type == businessType); 但是,我想做如下工作: customers = customers.Where(c => c.Business_Typ
customers = customers.Where(c => c.Business_Type == businessType);
但是,我想做如下工作:
customers = customers.Where(c => c.Business_Type == businessType, c.CreationDate=c.CreationDate.Value.ToShortDateString());
LINQ并不意味着执行序列元素的突变。只需获取
Where
的返回值,并使用foreach
执行变异,这是处理此问题的惯用方法:
var customers = customers.Where(c => c.Business_Type == businessType).ToArray();
foreach(var c in customers)
{
c.CreationDate = c.CreationDate.Value.ToShortDateString();
}
LINQ并不意味着执行序列元素的突变。只需获取
Where
的返回值,并使用foreach
执行变异,这是处理此问题的惯用方法:
var customers = customers.Where(c => c.Business_Type == businessType).ToArray();
foreach(var c in customers)
{
c.CreationDate = c.CreationDate.Value.ToShortDateString();
}
回答你的问题,是的,你可以。在如下方法中打开表达式:customers=customers.Where(c=>{bool r=c.Business_Type==businessType;c.CreationDate=c.CreationDate.Value.ToShortDateString();return r});不是建议你回答你的问题,是的,你可以。在如下方法中打开表达式:customers=customers.Where(c=>{bool r=c.Business_Type==businessType;c.CreationDate=c.CreationDate.Value.ToShortDateString();return r});并不是说它是被推荐的。。。