c.CompareListAndDataTable

PHOTO EMBED

Fri Oct 08 2021 17:17:05 GMT+0000 (UTC)

Saved by @rick_m #c#

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		List<QueueItem> queueItems = new List<QueueItem>() { new QueueItem("test1"), new QueueItem("test2") };
		var dataTableColumnName = "MainColumn";
		var dataTableToCompare = new DataTable();
		var dc = new DataColumn(dataTableColumnName);
		dataTableToCompare.Columns.Add(dc);
		
        DataRow dr = dataTableToCompare.NewRow();
                dr[dataTableColumnName] = "test1";
                dataTableToCompare.Rows.Add(dr);
		
		dr = dataTableToCompare.NewRow();
                dr[dataTableColumnName] = "test3";
                dataTableToCompare.Rows.Add(dr);
		
		dr = dataTableToCompare.NewRow();
                dr[dataTableColumnName] = "test4";
                dataTableToCompare.Rows.Add(dr);
		
		var queueNames = queueItems.Select(s => String.Concat("'",s.QueueName,"'")).ToList();
        DataRow[] filtered = dataTableToCompare.Select(String.Format("{0} NOT IN ({1}) ",dataTableColumnName,String.Join(",", queueNames)));
		
		  foreach(DataRow r in filtered)
                {
                    Console.WriteLine(r[dataTableColumnName].ToString());
                }
		
	}
}


public class QueueItem{
 public string QueueName{get;set;}
	
	public QueueItem(string queueName){
	
		QueueName = queueName;
		
	}
}
content_copyCOPY