使用LINQ更新实体列表中的属性 [英] Using LINQ to Update A Property in a List of Entities

查看:91
本文介绍了使用LINQ更新实体列表中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个看起来像这个简单示例的实体:

Say I have an entity that looks something like this simple example:

MyEntity 
{
   int property1;
   int property2;
   int property3;
}

现在假设我有这些实体的IEnumerable列表.我可以执行一个LINQ查询,该查询将为列表中的每个实体将property1的值设置为100吗?我知道我可以通过foreach来做到这一点,但是我想知道LINQ是否可以更优雅地做到这一点.

Now assume I have an IEnumerable list of these entites. Is there a LINQ query I can execute that would set the value of property1 to 100 for each entity in the list? I know I can do this via a foreach, but was wondering if LINQ could do this more elegantly.

推荐答案

像这样:

var result = GetMyIEnumerable()
                .ToList();
result.ForEach(x => x.property1 = 100);

这篇关于使用LINQ更新实体列表中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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