LINQ:选择一个对象并更改某些属性而不创建新对象 [英] LINQ: Select an object and change some properties without creating a new object

查看:22
本文介绍了LINQ:选择一个对象并更改某些属性而不创建新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改 LINQ 查询结果对象的某些属性,而无需创建新对象并手动设置每个属性.这可能吗?

I want to change some properties of a LINQ query result object without creating a new object and manually setting every property. Is this possible?

示例:

var list = from something in someList
           select x // but change one property

推荐答案

我不确定查询语法是什么.但这是扩展的 LINQ 表达式示例.

I'm not sure what the query syntax is. But here is the expanded LINQ expression example.

var query = someList.Select(x => { x.SomeProp = "foo"; return x; })

这样做是使用匿名方法与表达式.这允许您在一个 lambda 中使用多个语句.所以你可以将设置属性和返回对象这两个操作结合到这个有点简洁的方法中.

What this does is use an anonymous method vs and expression. This allows you to use several statements in one lambda. So you can combine the two operations of setting the property and returning the object into this somewhat succinct method.

这篇关于LINQ:选择一个对象并更改某些属性而不创建新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆