一次更新多个元素 LINQ

发布于 2024-10-20 01:24:43 字数 218 浏览 1 评论 0原文

是否可以使用 LINQ 对 List 中的每个元素设置属性。

例如:

var users = from u in context.Users where u.Name == "George" select u;

foreach (User us in users){
   us.MyProp = false;
}

是否可以使其更清洁?

Is it possible to set property on each element from List using LINQ.

for example:

var users = from u in context.Users where u.Name == "George" select u;

foreach (User us in users){
   us.MyProp = false;
}

Is it possible to make it cleaner ?

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

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

发布评论

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

评论(6

回忆凄美了谁 2024-10-27 01:24:43

或者您可以将其转换为 ToList() 并使用 ForEach 方法。

users.ToList().ForEach(u => u.MyProp = false);

更新多个属性

users.ToList().ForEach(u =>
                      {
                         u.property1 = value1;
                         u.property2 = value2;
                      });

或者像这样

(from u in context.Users where u.Name == "George" select u).ToList()
.ForEach(u => u.MyProp = false);

Or you can convert it to ToList() and use ForEach method.

users.ToList().ForEach(u => u.MyProp = false);

to update more than one properties

users.ToList().ForEach(u =>
                      {
                         u.property1 = value1;
                         u.property2 = value2;
                      });

Or like this

(from u in context.Users where u.Name == "George" select u).ToList()
.ForEach(u => u.MyProp = false);
安穩 2024-10-27 01:24:43

您可以创建自己的 ForEach 扩展方法:

public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
    foreach (var element in source)
    {
        action(element);
    }

    return source;
}

然后您的代码可以简化为:

context.Users.Where(x => x.Name == "George").ForEach(u => u.MyProp = false);

编辑:您还可以在调用 action() (没有双关语)后返回每个项目,以保持延迟执行不变。

You can create your own ForEach extension method:

public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
    foreach (var element in source)
    {
        action(element);
    }

    return source;
}

Then your code can be simplified to:

context.Users.Where(x => x.Name == "George").ForEach(u => u.MyProp = false);

EDIT: You could also yield return each item after the call to action() (no pun intended) to leave the deferred execution untouched.

旧城烟雨 2024-10-27 01:24:43

什么是更干净是相当主观的。就我个人而言,我发现这种方法在清晰度和简单性方面很难被击败:

foreach (User us in users.Where(u => u.Name == "George"))
{
   us.MyProp = false;
}

What is cleaner is quite subjective. Personally, I find this approach hard to beat for clarity and simplicity:

foreach (User us in users.Where(u => u.Name == "George"))
{
   us.MyProp = false;
}
送舟行 2024-10-27 01:24:43

如果您对内存更新感兴趣,即数据库不会更新,那么您可以使用

var users = (from u in context.Users where u.Name == "George" select u).ToList();
users.ForEach(u => u.MyProp = "Something");

If you're interested in in-memory update, that is the database is not going to be updated then you could use

var users = (from u in context.Users where u.Name == "George" select u).ToList();
users.ForEach(u => u.MyProp = "Something");
呆萌少年 2024-10-27 01:24:43

我会这样做

var users = from u in context.Users where u.Name == "George" 
select new User() 
{
   prop1 = u.prop1,
   prop2 = u.prop2,
   prop3 = true // update like here
};

看起来很脏,但如果询问(仅通过 select),如果您希望使用其他 C# 函数,

var users = context.Users.Where(u => u.Name == "George").ToList()
    .Select(p => new User() 
    {
       prop1 = p.prop1,
       prop2 = p.prop2,
       prop3 = ComputeHash(p) // update like here
    });

,您也可以使用 linq 函数并处理 IEnumerable 类型的列表。由于 .ToList().Select() 之前,上面的代码变成了 IEnumerable

Looks dirty but if asked (just via select), I'll do it this way

var users = from u in context.Users where u.Name == "George" 
select new User() 
{
   prop1 = u.prop1,
   prop2 = u.prop2,
   prop3 = true // update like here
};

if you wish to use some other C# function, you can also use linq functions and work on IEnumerable type of lists.

var users = context.Users.Where(u => u.Name == "George").ToList()
    .Select(p => new User() 
    {
       prop1 = p.prop1,
       prop2 = p.prop2,
       prop3 = ComputeHash(p) // update like here
    });

Code above becomes IEnumerable because of the .ToList() prior to the .Select()

酒与心事 2024-10-27 01:24:43

怎么样:

 context.Users.Where(u => u.Name == "George").Select(u => u.MyProp = false);

How about:

 context.Users.Where(u => u.Name == "George").Select(u => u.MyProp = false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文