更新/修剪 Lambda where 表达式中的值

发布于 2025-01-06 16:58:55 字数 413 浏览 1 评论 0原文

我有一个 Lambda where 表达式,它根据传入的 ID 过滤客户列表。这工作正常,但是我想在返回记录时从 CreationDate 字段中删除时间戳。有没有办法在 Lambda 表达式中执行此操作?

这是我的 Lambda 表达式,它返回我的客户记录:

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

但是我想做如下操作:

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

I have a Lambda where expression which filters a list of customer based on a ID passed in. This works fine however I want to remove the timestamp from the CreationDate field when I return the records. Is there anyway to do this within the Lambda expression?

So this is my Lambda expression which returns my customer records:

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

However I'd like to do something like the following:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉墨 2025-01-13 16:58:55

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 is not meant to perform mutation of sequence elements. Just take the return value of Where and use foreach to perform the mutation, which is the idiomatic way to handle this:

var customers = customers.Where(c => c.Business_Type == businessType).ToArray();
foreach(var c in customers)
{
    c.CreationDate = c.CreationDate.Value.ToShortDateString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文