출처1 : http://blog.daum.net/starkcb/65

출처2 : http://stackoverflow.com/questions/3829129/use-linq-to-group-data-from-datatable#3829196



using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace arrayTest

{

 

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        List<Test> list = new List<Test>();

        List<Test> listB = new List<Test>();

        DataTable dtA = new DataTable();

        DataTable dtB = new DataTable();

 

        private void Form1_Load(object sender, EventArgs e)

        {

            for (int i = 0; i < 10; i++)

            {

                list.Add(new Test { Na = "A" + i });

            }

            for (int i = 0; i < 5; i++)

            {

                listB.Add(new Test { Na = "A" + i });

            }

 

            dtA.Columns.Add("Name");

            for (int i = 0; i < 10; i++)

            {

                dtA.Rows.Add("A" + i);

            }

 

            dtB.Columns.Add("Name");

            for (int i = 0; i < 5; i++)

            {

                dtB.Rows.Add("A" + i);

            }

 

            //배열과 배열

            var obj = from n in list

                      let places = from p in listB select p.Na

                      where places.Contains(n.Na) // 없는 것을 찾으려면(not in) !places.Contains(n.Na)

                      select n;

 

            //배열과 데이터테이블

            var obj2 = from n in list

                       let places = from p in dtB.AsEnumerable() select p.Field<string>("Name")

                       where places.ToList<string>().Contains(n.Na) // 없는 것을 찾으려면(not in) !places.ToList<string>().Contains(n.Na)

                       select n;

 

            //데이터테이블과 배열

            var obj3 = from n in dtA.AsEnumerable()

                       let places = from p in listB select p.Na

                       where places.Contains(n.Field<string>("Name"))

                       select n;

 

            //테이터테이블과 테이터테이블

            var obj4 = from n in dtA.AsEnumerable()

                       let places = from p in dtB.AsEnumerable() select p.Field<string>("Name")

                       where places.ToList<string>().Contains(n.Field<string>("Name"))

                       select n;

        }

    }

    public class Test

    {

        public string Na { get; set; }

    }

}

 
























Posted by motolies

댓글을 달아 주세요