2015/01/28

C#.Net LINQ select new

Select new可以將資料取出,並且放入集合內
該集合可以自訂該屬性名稱

Code:
using System;
using System.Windows.Forms;
using System.Linq;
namespace Sample
{
    public partial class Form1 : Form
    {
        String[] names = { "a", "b", "c", "d", "e","f","g","h" };

        public Form1()
        {
            InitializeComponent();
            var gp = from n in names
                     select new
                     {
                         name = n,
                         index = Array.FindIndex(names, f => f.Contains(n))
                     };

            foreach (var g in gp)
                textBox1.Text += String.Format("{0}={1}{2}", 
                    g.index, g.name, Environment.NewLine);
        }

    }

}


執行結果: