Question32872161

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Collections.ObjectModel;
using System.Threading;
using System.ComponentModel;
using System.Xml.Linq;
using System.Reflection;
using System.Data;  // For System.Data.DataTableExtensions
using System.Xml.Schema;
using System.Runtime.Serialization;
using System.Globalization;
	

    public class TestClass
    {
        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:complexType>
                     <xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
                        <xs:element name=""Table"">
                           <xs:complexType>
                              <xs:sequence>
                                 <xs:element name=""SourceID"" type=""xs:string"" minOccurs=""0""/>
                                 <xs:element name=""Caption"" type=""xs:string"" minOccurs=""0""/>
                              </xs:sequence>
                           </xs:complexType>
                        </xs:element>
                     </xs:choice>
                  </xs:complexType>
               </xs:element>
            </xs:schema>
";
            return xsd;
        }

        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);
            }

            // Add boundary condition rows
            dataSet.Tables["Table"].Rows.Add(dataSet.Tables["Table"].NewRow());

            var badRow = dataSet.Tables["Table"].NewRow();
            badRow["Caption"] = "MissingCaption";
            dataSet.Tables["Table"].Rows.Add(badRow);

            return dataSet;
        }

        public static void Test()
        {
            var ds = GetDataSourceMappingTable();

            {
                var DataSourceId = ds.Tables["Table"]
                    .Select("Caption = 'Users_test'") // Find rows with Caption = 'Users_test'
                    .Select(r => r["SourceID"])       // Project to the value of SourceId
                    .Where(s => s != DBNull.Value)    // Filter DBNull (might occur when the SourceID cell is missing
                    .Select(s => s.ToString())        // Project to string value
                    .SingleOrDefault();               // null if no matches, throw an exception if more than one match.
                Console.WriteLine(DataSourceId);
            }

            {
                var query = ds.Tables["Table"]
                    .Select("Caption = 'Users_test'") // Find rows with Caption = 'Users_test'
                    .Select(r => r["SourceID"])       // Project to the value of SourceId
                    .Where(s => s != DBNull.Value)    // Filter DBNull (might occur when the SourceID cell is missing
                    .Select(s => s.ToString());        // Project to string value
                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())
                .SingleOrDefault();
            if (MissingDataSourceId != null)
            {
                Debug.Assert(MissingDataSourceId == null);
                throw new ArgumentNullException();
            }
        }
    }

	public class Program
	{
		public static void Main()
		{
			TestClass.Test();
		}
	}
;