CheckMate RPA

CheckMate RPA C# DataTable LinQ 적용하기 (System.Data.DataSetExtensions.dll)

밥탄이 2023. 4. 7. 16:50
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;

// LinQ 사용
using System.Linq;

public partial class CustomScript
{
	public void Execute_Code()
	{
		DataTable table = new DataTable();
		table.Reset();
		table.AcceptChanges();
		
		table.Columns.Add("lot");
		table.Columns.Add("data",typeof(double));
		
		table.Rows.Add("A", 45.2);
		table.Rows.Add("A", 45.8);
		table.Rows.Add("A", 45.9);
		table.Rows.Add("B", 45.1);
		table.Rows.Add("B", 45.5);
		
        // 저장하기
		table.AcceptChanges();
		
		// DataTable 데이터를 LinQ 쿼리 절에서 From 소스로 사용하기위해 AsEnumerable 메서드를 호출해야함 (열거)
		dt = table.AsEnumerable()
			.Where(Row=>
			       Row.Field<string>("lot") == "A" &&
			       Row.Field<double>("data") >= 45.5 && 
				Row.Field<string>("lot").Contains("A"))
			.OrderByDescending(Row=>
			                   Row.Field<double>("data"))
			.CopyToDataTable();
	}
}

LinQ 사용하기위해선 System.Data.DataSetExtensions.dll 필요하며

선언문에 using System.Linq; 도 포함해야한다.

 

예제를 위해 간단하게 2col , 5row를 선언해주고

Where 조건절에

Row.Field<데이터형식>("컬럼명") == 조건 && or || 등

조건을 선언해주면된다.

 

정렬이 필요한 경우 OrderByDescending 을 포함하여 특정 컬럼으로 정렬을 진행한다.

 

 

결과보기

더보기
LinQ 적용 전

 

LinQ 적용 후