使用 linq 赋值

发布于 2024-10-24 04:45:44 字数 333 浏览 1 评论 0原文

public class Company
{
    public int id { get; set; }
    public int Name { get; set; }
}

List<Company> listofCompany = new List<Company>();

这是我的公司列表集合,我想使用 LINQ 为 Name 属性分配值

listofCompany.Where(d => d.Id = 1);

(我想分配公司 id 1 的 name 属性)

如何分配它。

public class Company
{
    public int id { get; set; }
    public int Name { get; set; }
}

List<Company> listofCompany = new List<Company>();

this is my collection of company list I want to assign values to Name property using LINQ

listofCompany.Where(d => d.Id = 1);

(I want to assing name property of company id 1)

how do I assign it.?

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

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

发布评论

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

评论(4

三生一梦 2024-10-31 04:45:44

使用 Linq 将是:

 listOfCompany.Where(c=> c.id == 1).FirstOrDefault().Name = "Whatever Name";

UPDATE

这可以简化为...

 listOfCompany.FirstOrDefault(c=> c.id == 1).Name = "Whatever Name";

UPDATE

对于多个项目(多个项目满足条件):

 listOfCompany.Where(c=> c.id == 1).ToList().ForEach(cc => cc.Name = "Whatever Name");

using Linq would be:

 listOfCompany.Where(c=> c.id == 1).FirstOrDefault().Name = "Whatever Name";

UPDATE

This can be simplified to be...

 listOfCompany.FirstOrDefault(c=> c.id == 1).Name = "Whatever Name";

UPDATE

For multiple items (condition is met by multiple items):

 listOfCompany.Where(c=> c.id == 1).ToList().ForEach(cc => cc.Name = "Whatever Name");
攀登最高峰 2024-10-31 04:45:44

您可以创建一个扩展方法:

public static IEnumerable<T> Do<T>(this IEnumerable<T> self, Action<T> action) {
    foreach(var item in self) {
        action(item);
        yield return item;
    }
}

然后在代码中使用它:

listofCompany.Do(d=>d.Id = 1);
listofCompany.Where(d=>d.Name.Contains("Inc")).Do(d=>d.Id = 1);

You can create a extension method:

public static IEnumerable<T> Do<T>(this IEnumerable<T> self, Action<T> action) {
    foreach(var item in self) {
        action(item);
        yield return item;
    }
}

And then use it in code:

listofCompany.Do(d=>d.Id = 1);
listofCompany.Where(d=>d.Name.Contains("Inc")).Do(d=>d.Id = 1);
咆哮 2024-10-31 04:45:44

也可以这样做

foreach (Company company in listofCompany.Where(d => d.Id = 1)).ToList())
                {
                    //do your stuff here
                    company.Id= 2;
                    company.Name= "Sample"
                }

It can be done this way as well

foreach (Company company in listofCompany.Where(d => d.Id = 1)).ToList())
                {
                    //do your stuff here
                    company.Id= 2;
                    company.Name= "Sample"
                }
如梦 2024-10-31 04:45:44

请注意,它仅更新找到的第一家公司 ID 为 1 的公司。 对于多个

 (from c in listOfCompany where c.id == 1 select c).First().Name = "Whatever Name";

对于多个更新

 from c in listOfCompany where c.id == 1 select c => {c.Name = "Whatever Name";  return c;}

Be aware that it only updates the first company it found with company id 1. For multiple

 (from c in listOfCompany where c.id == 1 select c).First().Name = "Whatever Name";

For Multiple updates

 from c in listOfCompany where c.id == 1 select c => {c.Name = "Whatever Name";  return c;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文