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

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

问题描述

使用LINQ,如果我想执行一些查询,从查询返回的对象,而只是改变一些在该对象的属性,我将如何做到这一点,而无需创建新的对象,并手动设置每个属性?这可能吗?

例如:

 无功名单=从someList东西
           选择[x] //但是改变一个属性


解决方案

我不知道的查询语法是什么。但这里是扩大LINQ前pression例子。

  VAR的查询= someList.Select(X => {x.SomeProp =富;返回X;})

这样做是使用匿名方法VS和前pression。这可以让你使用多个语句在一个拉姆达。所以,你可以将设置该属性和对象返回到此有点简洁的方法,这两种操作。

Using LINQ, if I wanted to perform some query and return the object from the query, but change only some of the properties in that object, how would I do this without creating a new object and manually set every property? Is this possible?

Example:

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

解决方案

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; })

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天全站免登陆