使用 LINQ 查询和更新集合中的属性

发布于 2024-10-22 06:26:21 字数 534 浏览 1 评论 0 原文

使用 LINQ 更新集合中特定项目的单个属性的最简洁/简单的方法是什么?

例如,如果我有以下列表:

public class Ticket
{
    public string Name { get; set; }
    public string Code { get; set; }
    public bool Selected { get; set; }
}

如何使用 LINQ 更新票证项目的“Selected”属性,其中“Name”属性的值为“Beach”。在 SQL 中,它会是:

UPDATE Tickets SET Selected = true WHERE Name = 'Beach'

我认为我走在正确的轨道上......

tickets.Select(x => { x.Selected = true; return x; }).ToList().Where(x => x.Name == "Beach");

What is the most succint/simple way of updating a single property of a specific item within a collection using LINQ?

For example if I have a List of the following:

public class Ticket
{
    public string Name { get; set; }
    public string Code { get; set; }
    public bool Selected { get; set; }
}

How can I use LINQ to update the "Selected" property of a Ticket item wheres its "Name" property has the value of "Beach". In SQL it would be:

UPDATE Tickets SET Selected = true WHERE Name = 'Beach'

I thought I was on the right track with this...

tickets.Select(x => { x.Selected = true; return x; }).ToList().Where(x => x.Name == "Beach");

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

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

发布评论

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

评论(3

坐在坟头思考人生 2024-10-29 06:26:21

您可以更改顺序,然后使用 ForEach 运算符:

tickets
   .Where(x => x.Name == "Beach")
   .ToList()
   .ForEach(x => { x.Selected = true; });

注意:

  • 需要 ToList() ,因为 IEnumerable 不支持 Linq 中的 ForEach - 请参阅 LINQ 相当于 IEnumerable 的 foreach
  • 为了便于阅读,最好将其分离为 linq 查询,然后更传统的 foreach(x in list) C# 循环
  • ,如果这是 linq-to-sql,那么您需要调用 SubmitChanges() 来保存更改。

You can change the order, then use the ForEach operator:

tickets
   .Where(x => x.Name == "Beach")
   .ToList()
   .ForEach(x => { x.Selected = true; });

Note:

  • that the ToList() is needed because IEnumerable doesn't support ForEach in Linq - see LINQ equivalent of foreach for IEnumerable<T>
  • that for readability it might be better to separate this out into a linq query and then a more conventional foreach(x in list) C# loop
  • if this is linq-to-sql, then you'll need to call SubmitChanges() in order to persist your changes.
说好的呢 2024-10-29 06:26:21

回答有点晚了,但我认为我们不必转换为 ToList() 正如斯图尔特提到的那样,实际上我们能做的只是调整斯图尔特代码(这是一段很棒的代码),如下所示

tickets
   .Where(x => x.Name == "Beach")
   .All(x => x.Selected = true);

另一种选择

tickets
   .Where(x => x.Name == "Beach")
   .All(x => { x.Selected = true; return true; });

Bit late for an answer but i think we don't have to convert to ToList() as Mentioned by stuart what actually we can do is just tweak Stuart code(which is great piece of code) as following

tickets
   .Where(x => x.Name == "Beach")
   .All(x => x.Selected = true);

Another option

tickets
   .Where(x => x.Name == "Beach")
   .All(x => { x.Selected = true; return true; });
一世旳自豪 2024-10-29 06:26:21

首先我要说的是,不要使用 LINQ 来设置这样的属性,这不是 LINQ 的用途。

应该编写查询来选择要更改的行,在循环中更改它们,然后将更改提交到数据库(如果是 LINQ to SQL)。

var query = tickets.Where(ticket => ticket.Name == "Beach");
foreach (var item in query)
    item.Selected = true;

// if LINQ to SQL
context.SubmitChanges();

Let me start off by saying this, don't use LINQ to set properties like that, that's not how LINQ's meant to be used.

You should write the query to select the rows to be changed, change them in a loop, and submit the changes to the database (if LINQ to SQL).

var query = tickets.Where(ticket => ticket.Name == "Beach");
foreach (var item in query)
    item.Selected = true;

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