English 中文(简体)
将所有可数据栏目划入一个阵列
原标题:Find a string in all DataTable columns

I am trying to find a fast way to find a string in all datatable columns! Followed is not working as I want to search within all columns value.

string str = "%whatever%";
foreach (DataRow row in dataTable.Rows)
    foreach (DataColumn col in row.ItemArray)
        if (row[col].ToString() == str) return true;
最佳回答

可以通过过滤来实现这一点。 1. 根据所有栏目建立(可使用)过滤器:

        bool UseContains = false;
        int colCount = MyDataTable.Columns.Count;


        string likeStatement = (UseContains) ? " Like  %{0}% " : " Like  {0}% "; 
        for (int i = 0; i < colCount; i++)
        {
            string colName = MyDataTable.Columns[i].ColumnName;
            query.Append(string.Concat("Convert(", colName, ",  System.String )", likeStatement));


            if (i != colCount - 1)
                query.Append(" OR ");
        }

        filterString = query.ToString();

现在,你可以浏览一下哪一栏与你的搜索相匹配:

 string currFilter = string.Format(filterString, searchText);
 DataRow[] tmpRows = MyDataTable.Select(currFilter, somethingToOrderBy);
问题回答

你们可以使用LINQ。 这样做不会更快,因为如果价值没有,你仍需要看每个囚室,但会符合一条:

return dataTable
    .Rows
    .Cast<DataRow>()
    .Any(r => r.ItemArray.Any(c => c.ToString().Contains("whatever")));

为了寻找随机案文,并归还至少有一个对个案不敏感的囚室的一系列行文:

var text = "whatever";
return dataTable
    .Rows
    .Cast<DataRow>()
    .Where(r => r.ItemArray.Any(
        c => c.ToString().IndexOf(text, StringComparison.OrdinalIgnoreCase) > 0
    )).ToArray();

如果你想检查你每栏一栏每栏一行。 数据表,尝试(我的工作!)

DataTable YourTable = new DataTable();
// Fill your DataTable here with whatever you ve got.

foreach (DataRow row in YourTable.Rows)
{
    foreach (object item in row.ItemArray)
    {
        //Do what ya gotta do with that information here!
    }
}

Don tabes to打字object items to what per cent (string, int等)。

我赶上了挖尸首,并做药。 我希望这一帮助和好uck!

你们也可以用一系列星号进行例行搜查:

string[] elems = {"GUID", "CODE", "NAME", "DESCRIPTION"};//Names of the columns             
foreach(string column in elems)
{
    string expression = string.Format("{0} like  %{1}% ",column, 
    txtSearch.Text.Trim());//Search Expression
    DataRow[] row = data.Select(expression);
    if(row.Length > 0) {
        // Some code here
    } else {
        // Other code here
    }
 }

你可以通过使用哥伦纳梅方法获得各栏的名称。 然后,可以通过使用数据检索每个栏目。 例如,环礁法典将发挥作用。

string str = "whatever";
foreach (DataRow row in dataTable.Rows)
{
    foreach (DataColumn column in dataTable.Columns)
    {
        if (row[column.ColumnName.ToString()].ToString().Contains(str))
        {
            return true; 
        }
    }
}

你们也可以在数据表上形成一个过滤的表述。 见http://msdn.microsoft.com/en-us/library/zk13kdh0%28v=vs.71%29.aspx“rel=“nofollow”>。 就像在你的过滤表达中使用。

 string filterExp = "Status =  Active ";
 string sortExp = "City";
 DataRow[] drarray;
 drarray = dataSet1.Customers.Select(filterExp, sortExp, DataViewRowState.CurrentRows);
 for (int i=0; i < drarray.Length; i++)
 {
    listBox1.Items.Add(drarray[i]["City"].ToString());
 }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签