[英]How to set a property value to null when select items in a list
I have a class Like this,我有一个这样的课程,
class Test
{
public int ID { get; set; }
public string Name { get; set; }
}
I want to select eligible items in List, I also want to set Name to null of the result list.我想在列表中选择符合条件的项目,我还想将结果列表的 Name 设置为 null。 I don't want to loop the collection again to set property value, is there a way to set the property value during the selection in linq?
我不想再次循环集合来设置属性值,有没有办法在 linq 中的选择过程中设置属性值?
This is my current query:这是我当前的查询:
var v = from t in tests where ids.Contains(t.ID) select t;
Thanks谢谢
You can set it to null
explicitly:您可以将其显式设置为
null
:
var newTests = tests.Where(test => ids.Contains(test.ID))
.Select(test => {
test.Name = null;
return test;
});
Or you can project new instances of Test
:或者您可以投影
Test
新实例:
var newTests = tests.Where(test => ids.Contains(test.ID))
.Select(test => new Test { ID = test.ID });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.