當前位置: 首頁>>代碼示例>>C#>>正文


C# DataTable.ToObject方法代碼示例

本文整理匯總了C#中System.Data.DataTable.ToObject方法的典型用法代碼示例。如果您正苦於以下問題:C# DataTable.ToObject方法的具體用法?C# DataTable.ToObject怎麽用?C# DataTable.ToObject使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Data.DataTable的用法示例。


在下文中一共展示了DataTable.ToObject方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: DifferentTypes

        public void DifferentTypes()
        {
            // Arrange
            var table = new DataTable();

            table.Columns.Add("Id", typeof(int)); // Should be a Guid
            table.Columns.Add("Date", typeof(DateTime));
            table.Columns.Add("Enum", typeof(int));
            table.Columns.Add("Text", typeof(string));
            table.Columns.Add("Array", typeof(int[]));

            table.Rows.Add(
                11,
                new DateTime(1999, 1, 1),
                Enumeration.One,
                "eleven",
                new[] { 1, 2, 3, }
            );

            // Act
            table.ToObject<RecursiveData>().Single();
        }
開發者ID:tallesl,項目名稱:net-Object,代碼行數:22,代碼來源:DataTableTests.cs

示例2: DataTableToObjectTest

        public void DataTableToObjectTest()
        {
            var table = new DataTable();
            table.Columns.AddRange(new DataColumn[]{
                new DataColumn("Int",typeof(int)),
                new DataColumn("String",typeof(string)),
            });

            table.Rows.Add(1, "Test1");
            table.Rows.Add(2, "Test2");

            var test = table.ToObject<TestClass>().Select((obj, count) => Tuple.Create(obj, count));

            foreach (var tpl in test) {
                if (tpl.Item2 == 0) {
                    Assert.AreEqual(tpl.Item1.Int, 1);
                    Assert.AreEqual(tpl.Item1.String, "Test1");
                } else if (tpl.Item2 == 1) {
                    Assert.AreEqual(tpl.Item1.Int, 2);
                    Assert.AreEqual(tpl.Item1.String, "Test2");
                }
            }
        }
開發者ID:azkey,項目名稱:AzLib,代碼行數:23,代碼來源:DataTableExtensionTest.cs

示例3: LengthyTest


//.........這裏部分代碼省略.........

            // 2nd level
            table.Columns.Add("Nested.Id", typeof(Guid));
            table.Columns.Add("Nested.Date", typeof(DateTime));
            table.Columns.Add("Nested.Enum", typeof(int));
            table.Columns.Add("Nested.Text", typeof(string));
            table.Columns.Add("Nested.Array", typeof(int[]));

            // 3rd level
            table.Columns.Add("Nested.Nested.Id", typeof(Guid));
            table.Columns.Add("Nested.Nested.Date", typeof(DateTime));
            table.Columns.Add("Nested.Nested.Enum", typeof(int));
            table.Columns.Add("Nested.Nested.Text", typeof(string));
            table.Columns.Add("Nested.Nested.Array", typeof(int[]));

            // 4th level
            table.Columns.Add("Nested.Nested.Nested.Id", typeof(Guid));
            table.Columns.Add("Nested.Nested.Nested.Date", typeof(DateTime));
            table.Columns.Add("Nested.Nested.Nested.Enum", typeof(int));
            table.Columns.Add("Nested.Nested.Nested.Text", typeof(string));
            table.Columns.Add("Nested.Nested.Nested.Array", typeof(int[]));

            // 5th level
            table.Columns.Add("Nested.Nested.Nested.Nested.Id", typeof(Guid));
            table.Columns.Add("Nested.Nested.Nested.Nested.Date", typeof(DateTime));
            table.Columns.Add("Nested.Nested.Nested.Nested.Enum", typeof(int));
            table.Columns.Add("Nested.Nested.Nested.Nested.Text", typeof(string));
            table.Columns.Add("Nested.Nested.Nested.Nested.Array", typeof(int[]));

            table.Rows.Add(

                // 1st level
                expected.Id,
                expected.Date,
                expected.Enum,
                expected.Text,
                expected.Array,

                // 2nd level
                expected.Nested.Id,
                expected.Nested.Date,
                expected.Nested.Enum,
                expected.Nested.Text,
                expected.Nested.Array,

                // 3rd level
                expected.Nested.Nested.Id,
                expected.Nested.Nested.Date,
                expected.Nested.Nested.Enum,
                expected.Nested.Nested.Text,
                expected.Nested.Nested.Array,

                // 4th level
                expected.Nested.Nested.Nested.Id,
                expected.Nested.Nested.Nested.Date,
                expected.Nested.Nested.Nested.Enum,
                expected.Nested.Nested.Nested.Text,
                expected.Nested.Nested.Nested.Array,

                // 5th level
                DBNull.Value,
                DBNull.Value,
                DBNull.Value,
                DBNull.Value,
                DBNull.Value
            );

            var actual = table.ToObject<RecursiveData>().Single();

            // 1st level
            Assert.AreEqual(expected.Id, actual.Id);
            Assert.AreEqual(expected.Date, actual.Date);
            Assert.AreEqual(expected.Enum, actual.Enum);
            Assert.AreEqual(expected.Text, actual.Text);
            CollectionAssert.AreEqual(expected.Array, actual.Array);

            // 2nd level
            Assert.AreEqual(expected.Nested.Id, actual.Nested.Id);
            Assert.AreEqual(expected.Nested.Date, actual.Nested.Date);
            Assert.AreEqual(expected.Nested.Enum, actual.Nested.Enum);
            Assert.AreEqual(expected.Nested.Text, actual.Nested.Text);
            CollectionAssert.AreEqual(expected.Nested.Array, actual.Nested.Array);

            // 3rd level
            Assert.AreEqual(expected.Nested.Nested.Id, actual.Nested.Nested.Id);
            Assert.AreEqual(expected.Nested.Nested.Date, actual.Nested.Nested.Date);
            Assert.AreEqual(expected.Nested.Nested.Enum, actual.Nested.Nested.Enum);
            Assert.AreEqual(expected.Nested.Nested.Text, actual.Nested.Nested.Text);
            CollectionAssert.AreEqual(expected.Nested.Nested.Array, actual.Nested.Nested.Array);

            // 4th level
            Assert.AreEqual(expected.Nested.Nested.Nested.Id, actual.Nested.Nested.Nested.Id);
            Assert.AreEqual(expected.Nested.Nested.Nested.Date, actual.Nested.Nested.Nested.Date);
            Assert.AreEqual(expected.Nested.Nested.Nested.Enum, actual.Nested.Nested.Nested.Enum);
            Assert.AreEqual(expected.Nested.Nested.Nested.Text, actual.Nested.Nested.Nested.Text);
            CollectionAssert.AreEqual(expected.Nested.Nested.Nested.Array, actual.Nested.Nested.Nested.Array);

            // 5th level
            Assert.AreEqual(null, actual.Nested.Nested.Nested.Nested);
        }
開發者ID:tallesl,項目名稱:net-Object,代碼行數:101,代碼來源:DataTableTests.cs

示例4: Single

        public void Single()
        {
            // Arrange
            var expected = new SingleData { Id = new Guid("366f4bd3-6717-4b14-9c79-70515296df7e") };
            var table = new DataTable();
            table.Columns.Add("Id", typeof(Guid));
            table.Rows.Add(new Guid("366f4bd3-6717-4b14-9c79-70515296df7e"));

            // Act
            var actual = table.ToObject<SingleData>();

            // Assert
            Assert.AreEqual(1, actual.Count());
            Assert.AreEqual(expected.Id, actual.Single().Id);
        }
開發者ID:tallesl,項目名稱:net-Object,代碼行數:15,代碼來源:DataTableTests.cs

示例5: MissingProperty

        public void MissingProperty()
        {
            // Arrange
            var table = new DataTable();

            table.Columns.Add("Id", typeof(Guid));
            table.Columns.Add("Date", typeof(DateTime));
            table.Columns.Add("Enum", typeof(int));
            table.Columns.Add("Text", typeof(string));
            table.Columns.Add("Array", typeof(int[]));
            table.Columns.Add("Integer", typeof(int)); // There's no Integer

            table.Rows.Add(
                new Guid("366f4bd3-6717-4b14-9c79-70515296df7e"),
                new DateTime(1999, 1, 1),
                Enumeration.One,
                "eleven",
                new[] { 1, 2, 3, },
                11
            );

            // Act
            table.ToObject<RecursiveData>().Single();
        }
開發者ID:tallesl,項目名稱:net-Object,代碼行數:24,代碼來源:DataTableTests.cs


注:本文中的System.Data.DataTable.ToObject方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。