c.DataTableRowsNotInList

PHOTO EMBED

Fri Oct 08 2021 19:47:48 GMT+0000 (Coordinated Universal Time)

Saved by @rick_m #c#

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Data.DataSetExtensions;
public class Program
{
	public static void Main()
	{
		var dictionary1 = new Dictionary<string, object>();
		dictionary1.Add("Main Column", "test1");
		var dictionary2 = new Dictionary<string, object>();
		dictionary2.Add("Main Column", "test2");
		
		
		List<QueueItem> queueItems = new List<QueueItem>() { new QueueItem("QueueName", dictionary1), new QueueItem("QueueName", dictionary2) };
		string queueSpecificContentKey = "Main Column";
		
		
		var dataTableColumnName = "Main Column";
		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)));
		
		
		var filteredRows = from row in dataTableToCompare.AsEnumerable()
                                   join queueItem in queueItems
                                   on row.Field<string>(dataTableColumnName) equals queueItem.SpecificContent[queueSpecificContentKey] into rowLevelID
                                   from subrow in rowLevelID.DefaultIfEmpty()
                                   where subrow == null
                                   select row;
		
		  foreach(DataRow r in filteredRows)
                {
                    Console.WriteLine(r[dataTableColumnName].ToString());
                }
		
	}
}


public class QueueItem{
	
 public string QueueName{get;set;}
 public Dictionary<string, object> SpecificContent{get;set;}
	
	public QueueItem(string queueName, Dictionary<string, object> specificContent){
	
		QueueName = queueName;
		SpecificContent = specificContent;
		
	}
}
content_copyCOPY