using System.Collections.Generic;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Globalization;
public static string GetDataSetXsd()
var xsd = @"<?xml version=""1.0"" encoding=""utf-16""?>
<xs:schema id=""NewDataSet"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
<xs:element name=""NewDataSet"" msdata:IsDataSet=""true"" msdata:UseCurrentLocale=""true"">
<xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
<xs:element name=""Table"">
<xs:element name=""SourceID"" type=""xs:string"" minOccurs=""0""/>
<xs:element name=""Caption"" type=""xs:string"" minOccurs=""0""/>
public static DataSet GetDataSourceMappingTable()
DataSet dataSet = new DataSet();
using (var reader = new StringReader(GetDataSetXsd()))
dataSet.ReadXmlSchema(reader);
for (int i = 0; i < 10; i++)
var row = dataSet.Tables["Table"].NewRow();
row["SourceID"] = (i * 2).ToString();
row["Caption"] = "Users_test" + (i == 7 ? "" : i.ToString());
dataSet.Tables["Table"].Rows.Add(row);
dataSet.Tables["Table"].Rows.Add(dataSet.Tables["Table"].NewRow());
var badRow = dataSet.Tables["Table"].NewRow();
badRow["Caption"] = "MissingCaption";
dataSet.Tables["Table"].Rows.Add(badRow);
public static void Test()
var ds = GetDataSourceMappingTable();
var DataSourceId = ds.Tables["Table"]
.Select("Caption = 'Users_test'")
.Select(r => r["SourceID"])
.Where(s => s != DBNull.Value)
.Select(s => s.ToString())
Console.WriteLine(DataSourceId);
var query = ds.Tables["Table"]
.Select("Caption = 'Users_test'")
.Select(r => r["SourceID"])
.Where(s => s != DBNull.Value)
.Select(s => s.ToString());
foreach (var DataSourceId in query)
Console.WriteLine(" " + DataSourceId);
var MissingDataSourceId = ds.Tables["Table"]
.Select("Caption = 'MissingCaption'")
.Select(r => r["SourceID"])
.Where(s => s != DBNull.Value)
.Select(s => s.ToString())
if (MissingDataSourceId != null)
Debug.Assert(MissingDataSourceId == null);
throw new ArgumentNullException();
public static void Main()