0

我有一个数据集如下:

foo1, Name, foo2
==================
xxxx, ab01, xxxx
xxxx, ab02, xxxx
xxxx, ab11, xxxx    <---the 1st row of 'ab1%' in Name Column
xxxx, ab12, xxxx
xxxx, ab21, xxxx
xxxx, ab22, xxxx
xxxx, ab31, xxxx
xxxx, ab32, xxxx
xxxx, ab33, xxxx    <---the last row of 'ab3%' in Name Column
xxxx, ab41, xxxx

如何从上述数据集和“ab1%”和“ab3%”之间的“名称”列中获取新数据集。意思如下,

foo1, Name, foo2
==================
xxxx, ab11, xxxx
xxxx, ab12, xxxx
xxxx, ab21, xxxx  
xxxx, ab22, xxxx
xxxx, ab31, xxxx
xxxx, ab32, xxxx
xxxx, ab33, xxxx

非常感谢。

4

2 回答 2

0

你可以像下面这样使用:

var query = from c in DataTableName
where SqlMethods.Like(c.Name, "ab1%") && SqlMethods.Like(c.Name, "ab3%")
select c;

于 2013-04-30T09:07:43.417 回答
0

只需使用 Linq 来查询它 - 这有效(尽管它使用正则表达式进行匹配!!)请注意,您最感兴趣的是我创建“dataset2”的位置 - 这是您问题的答案

class Program
{
    static void Main(string[] args)
    {
        var dataset1 = CreateDataSet();
        var regex = new Regex(@"ab[1-3]{1}\d");

        //create dataset
        var oDS = new DataSet();
        var oTable = oDS.Tables.Add();
        oTable.Columns.Add("Foo1", typeof(int));
        oTable.Columns.Add("Name", typeof(string));
        oTable.Columns.Add("Foo2", typeof(int));

        var dataset2 = dataset1.Tables[0].AsEnumerable().Where(x => regex.IsMatch(x["Name"].ToString())).CopyToDataTable();

        foreach (DataRow row in dataset2.Rows)
        {
            Console.WriteLine(row["Name"]);
        }

        Console.ReadLine();
    }

    private static DataSet CreateDataSet()
    {
        //create dataset
        var oDS = new DataSet();
        // create datatable
        var oTable = oDS.Tables.Add();

        //Add coloms
        oTable.Columns.Add("Foo1", typeof(int));
        oTable.Columns.Add("Name", typeof(string));
        oTable.Columns.Add("Foo2", typeof(int));

        //Add rows
        oTable.Rows.Add(1, "ab01", 1);
        oTable.Rows.Add(2, "ab10", 2);
        oTable.Rows.Add(3, "ab11", 3);
        oTable.Rows.Add(4, "ab21", 4);
        oTable.Rows.Add(5, "ab22", 5);
        oTable.Rows.Add(6, "ab31", 6);
        oTable.Rows.Add(7, "ab32", 7);
        oTable.Rows.Add(8, "ab41". 8);

        return oDS;
    }   

}
于 2013-04-30T09:08:03.317 回答