開発環境 Microsoft Visual Studio Community 2017
実行環境 Microsoft Windows 10 Home (64-bit)
プロジェクトの種類 Visual C# / コンソール アプリ (.NET Framework)
プロジェクト名 WhereRen

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
 
namespace WhereRen
{
    class Parts
    {
        public int id;
        public string label;
 
        public Parts(int id, string label)
        {
            this.id = id;
            this.label = label;
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            List<Parts> parts = new List<Parts>()
            {
                new Parts(1, "tako"),
                new Parts(2, "ika"),
                new Parts(3, "manbo"),
                new Parts(4, "tako"),
                new Parts(5, "manbo"),
            };
 
            string searchText = "manbo";
            string renameText = "kani";
 
            foreach (var part in parts.Where(part => part.label == searchText))
            {
                part.label = renameText;
            }
 
            foreach (var part in parts)
            {
                Console.WriteLine("{0} : {1}", part.id, part.label);
            }
        }
    }
}
 

出力
1 : tako
2 : ika
3 : kani
4 : tako
5 : kani
 
最終更新:2018年05月25日 21:56