1)I have two datasets data1 and data2. It should be noted that my data1 contains 10 rows and data2 contains 50 rows. Both datasets have a column named ID.
How to check that the data1 of ID is contained in the data2 of ID?
if Id present in second datset coulmn then save in list1 and not found in second datset then save in list2.

Amit MohantyRank #112 · 20K reputationPosted Aug 9, 2023, 8:25 AM EDT
Check this
DataSet ExcelData = new DataSet(); dataAdapter.Fill(ExcelData); DataSet DataEmp = InProc.RetriveDataSet(Qry); List list1 = new List(); List list2 = new List(); foreach (DataRow row in ExcelData.Tables[0].Rows) { int id = Convert.ToInt32(row["ID"]); // Assuming "ID" is the column name in ExcelData if (DataEmp.Tables[0].AsEnumerable().Any(dataRow => dataRow.Field("Emp_no") == id)) { list1.Add(id); } else { list2.Add(id); } } Console.WriteLine("IDs present in DataEmp:"); foreach (var id in list1) { Console.WriteLine(id); } Console.WriteLine("IDs not present in DataEmp:"); foreach (var id in list2) { Console.WriteLine(id); }Amit MohantyRank #112 · 20K reputationPosted Aug 9, 2023, 5:17 AM EDT
using System; using System.Collections.Generic; using System.Linq; namespace DatasetComparison { class Program { static void Main(string[] args) { // Sample data List data1 = new List { new Data { ID = 1, Name = "Item 1" }, new Data { ID = 2, Name = "Item 2" }, // ... }; List data2 = new List { new Data { ID = 1, Name = "Item 1" }, new Data { ID = 3, Name = "Item 3" }, // ... }; // Find IDs present in data1 that are also present in data2 List list1 = data1.Where(d1 => data2.Any(d2 => d2.ID == d1.ID)).Select(d1 => d1.ID).ToList(); // Find IDs present in data1 that are not present in data2 List list2 = data1.Where(d1 => !data2.Any(d2 => d2.ID == d1.ID)).Select(d1 => d1.ID).ToList(); // Display the results Console.WriteLine("IDs present in both data1 and data2:"); foreach (int id in list1) { Console.WriteLine(id); } Console.WriteLine("IDs present in data1 but not in data2:"); foreach (int id in list2) { Console.WriteLine(id); } } } class Data { public int ID { get; set; } public string Name { get; set; } // Other properties of your data item } }AmitRank #9,090 · 211 reputationPosted Aug 9, 2023, 6:59 AM EDT
i am using this two dataset to compair Id column .
1) DataSet ExcelData = new DataSet();
dataAdapter.Fill(ExcelData);
2) string Qry = @"SELECT Emp_no FROM EmployeeTable";
DataSet DataEmp = InProc.RetriveDataSet(Qry);
First ExcelData have 100 record and second DataEmp 500 record. I want to compair first datset to second datset if first dataset value present in second datset that value put in list and if data not present in scond datset put in second list.
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 9, 2023, 5:15 AM EDT
try sample logic:
List data1 = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; List data2 = new List { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50 }; List list1 = new List(); List list2 = new List(); foreach (int id in data1) { if (data2.Contains(id)) { list1.Add(id); } else { list2.Add(id); } } Console.WriteLine("IDs present in data2:"); foreach (int id in list1) { Console.WriteLine(id); } Console.WriteLine("IDs not found in data2:"); foreach (int id in list2) { Console.WriteLine(id); }Cr BhargaviRank #420 · 5.9K reputationPosted Aug 9, 2023, 8:27 AM EDT
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main(string[] args) { // Sample data1 and data2 datasets List data1 = new List { new Data { ID = 1 }, new Data { ID = 5 }, // ... (add more data) }; List data2 = new List { new Data { ID = 3 }, new Data { ID = 5 }, // ... (add more data) }; // Check IDs in data1 against data2 and separate them into lists List list1 = new List(); List list2 = new List(); foreach (var item in data1) { if (data2.Any(d => d.ID == item.ID)) { list1.Add(item.ID); } else { list2.Add(item.ID); } } Console.WriteLine("IDs present in both datasets (list1):"); foreach (var id in list1) { Console.WriteLine(id); } Console.WriteLine("IDs not found in second dataset (list2):"); foreach (var id in list2) { Console.WriteLine(id); } } } class Data { public int ID { get; set; } }AmitRank #9,090 · 211 reputationPosted Aug 10, 2023, 2:21 AM EDT
int id = Convert.ToInt32(row["ID"]); // Assuming "ID" is the column name in ExcelData if (DataEmp.Tables[0].AsEnumerable().Any(dataRow => dataRow.Field("Emp_no") == id)) showing error in Specified cast is not valid.
Amit MohantyRank #112 · 20K reputationPosted Aug 10, 2023, 2:51 AM EDT
The error you're encountering ("Specified cast is not valid") occurs when there is a problem with the data type conversion. It's possible that the values in the "Emp_no" column of your DataEmp dataset are not valid integers. Try this
string id = row["ID"].ToString(); // Convert to string if (DataEmp.Tables[0].AsEnumerable().Any(dataRow => dataRow.Field("Emp_no") == id))