none
Create a data set from data table using a regular expression match

    Question

  • Hello,

    I need to Create a Dataset from a Data Table  and I would like to use a regular expression match in the filter. Can someone point me in the right direction.

    Bill

    Monday, October 14, 2013 3:39 PM

Answers

  • Is this acceptable?  Instead of using a DataTable.Select() medthod I used a litttle bit of Linq.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Text.RegularExpressions;
    namespace ConsoleApplication1
    {
        class Program
        {
            static string[] input = new string[] {
                  "123456-1abcdefghijklmnopqrstuvwxyz",
                  "123456-2abcdefghijklmnopqrstuvwxyz",
                  "123456-3abcdefghijklmnopqrstuvwxyz",
                  "123456-4abcdefghijklmnopqrstuvwxyz",
                  "123456-5abcdefghijklmnopqrstuvwxyz",
                  "123456-6abcdefghijklmnopqrstuvwxyz",
                  "1-23456abcdefghijklmnopqrstuvwxyz",
                  "12-34561abcdefghijklmnopqrstuvwxyz",
                  "123-4561abcdefghijklmnopqrstuvwxyz",
                  "1234-561abcdefghijklmnopqrstuvwxyz",
                  "12345-61abcdefghijklmnopqrstuvwxyz",
            };
            static void Main(string[] args)
            {
                DataTable table = new DataTable();
                table.Columns.Add("column A");
                foreach (string line in input)
                {
                    DataRow newRow = table.Rows.Add();
                    newRow["column A"] = line;
                }
                Regex SelectRegex = new Regex(@"\d{6}-\d{1}");
                
                DataTable matchedRows = table.AsEnumerable()
                    .Where(x => SelectRegex.IsMatch(x["column A"].ToString()) == true)
                    .CopyToDataTable();
                DataView results = matchedRows.DefaultView;
            }
        }
    }


    jdweng

    Tuesday, October 15, 2013 4:19 PM

All replies

  • Hi williamtholmes,

    About how to add a DataTable to a DataSet, please see:

    Adding a DataTable to a DataSet

    Here's also a MSDN document talking about DataSet and DataTable, you could find the relationship between the two objects:

    DataSets, DataTables, and DataViews

    About the filter, I think you need to clarify your question with detailed information:

    Do you want to add a DataTable with a specified name, or specified data to the DataSet?

    Do you just need a DataSet with some columns from the DataTable?

    About Regular Expression in .net, please check the following documents:

    Regular Expression Language - Quick Reference

    How to: Search Strings Using Regular Expressions (C# Programming Guide)


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, October 15, 2013 6:26 AM
    Moderator
  • You need to use a Linq statement.  The DataTable needs to use AsEnumerable() method and you will need to use AsDataView() to convert to a dataset.  I can give addional help as required I have done a lot of filtering of DataTables with Linq.

    The website below can get you started.

     

    http://stackoverflow.com/questions/10855/linq-query-on-a-datatable


    jdweng

    Tuesday, October 15, 2013 6:51 AM
  • Hi,

    I totally miswrote this question yesterday sorry.

    What I want to do is create a DefaultView for a DataTable with a filter that uses a regular expression, rather than a simple match.

    I have a column in the DataTable that I want to match using a regular expression. Is it possible to set the row filter to do this?

    The Regex I am using is:

    DataView qqq = zzz.DefaultView;
    

    Regex SelectRegex = newRegex(@"\d{6}-\d{1}");

    qqq.RowFilter = "column A value matches Select Regex"

     
    Tuesday, October 15, 2013 3:28 PM
  • Is this acceptable?  Instead of using a DataTable.Select() medthod I used a litttle bit of Linq.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Text.RegularExpressions;
    namespace ConsoleApplication1
    {
        class Program
        {
            static string[] input = new string[] {
                  "123456-1abcdefghijklmnopqrstuvwxyz",
                  "123456-2abcdefghijklmnopqrstuvwxyz",
                  "123456-3abcdefghijklmnopqrstuvwxyz",
                  "123456-4abcdefghijklmnopqrstuvwxyz",
                  "123456-5abcdefghijklmnopqrstuvwxyz",
                  "123456-6abcdefghijklmnopqrstuvwxyz",
                  "1-23456abcdefghijklmnopqrstuvwxyz",
                  "12-34561abcdefghijklmnopqrstuvwxyz",
                  "123-4561abcdefghijklmnopqrstuvwxyz",
                  "1234-561abcdefghijklmnopqrstuvwxyz",
                  "12345-61abcdefghijklmnopqrstuvwxyz",
            };
            static void Main(string[] args)
            {
                DataTable table = new DataTable();
                table.Columns.Add("column A");
                foreach (string line in input)
                {
                    DataRow newRow = table.Rows.Add();
                    newRow["column A"] = line;
                }
                Regex SelectRegex = new Regex(@"\d{6}-\d{1}");
                
                DataTable matchedRows = table.AsEnumerable()
                    .Where(x => SelectRegex.IsMatch(x["column A"].ToString()) == true)
                    .CopyToDataTable();
                DataView results = matchedRows.DefaultView;
            }
        }
    }


    jdweng

    Tuesday, October 15, 2013 4:19 PM