将字符串值列表与数据表进行比较 [英] Compare a list of string values with data table
问题描述
大家好,
我要求将文本框输入(其中包含由分隔符分隔的字母数字列表)与动态数据表中的数字列表进行比较。积极比较的结果是显示字母数字列表中的哪些数字位于警报弹出框中的数据表中。
如果列表中的所有容器都不匹配,则显示不同的警报标准。
我目前能够为一个输入字符串实现此目的。我只需要示例代码来分割字符串,存储在数组中并将其与datatable进行比较。谢谢。
public void Search_click( Object sender,EventArgs E)
{
string path = @ C:\ TEMP \SearchString.xlsx;
string connStr = Provider = Microsoft。 ACE.OLEDB.12.0;数据源= +路径+ ;扩展属性= Excel 12.0; 跨度>;
OleDbConnection objConn = new OleDbConnection(connStr);
尝试
{
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand( SELECT *来自MyRange,objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, XLData);
string strExcelInfo = ;
strInfoText = _hiddentextInfo.Value;
bool foundinExcel = false ;
if (objDataset1.Tables [ 0 ]。Rows.Count > 0 )
{
for ( int i = 0 ; i < ; objDataset1.Tables [ 0 ]。Rows.Count; i ++)
{
strExcelInfo = Convert.ToString(objDataset1.Tables [ 0 ]。行[i] [ Sheet1 跨度>]的ToString());
if (strInfoText.Contains(strExcelInfo))
{
foundinExcel = 真跨度>;
break ;
}
其他
{
继续;
}
}
}
如果(foundinExcel == true )
{
ClientScript.RegisterClientScriptBlock( this .GetType(), alert, alert(' + strInfoText + 在excel文件中'),真跨度>);
}
else if (foundinExcel = = false )
{
ClientScript.RegisterClientScriptBlock( this .GetType(), alert, alert(' + strInfoText + 不在excel文件中。 '), true );
}
}
最简单的方法之一是使用Linq to DataSet [ ^ ]。
void Main()
{
string myDelimitedText = A123,C521,A224,B221,;
DataTable dt = CreateDataTable();
var dl = myDelimitedText.Split( new char [] {' ,'},StringSplitOptions.RemoveEmptyEntries)
。选择(x =>新
{
Alpha = x,
IsInDt = dt.AsEnumerable()。任意(y => y.Field< string>(< span class =code-string> MyField)== x)
});
foreach ( var a in dl)
{
Console.WriteLine( {0} {1},a.Alpha,a.IsInDt);
}
}
// 在此处定义其他方法和类
public static DataTable CreateDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn( MyField,Type.GetType( System.String)));
dt.Rows.Add( new Object [] { A123});
dt.Rows.Add( new Object [] { B123});
dt.Rows.Add( new Object [] { C123});
dt.Rows.Add( new Object [] { A224});
dt.Rows.Add( new Object [] { C324});
return dt;
}
结果:
Alpha IsInDt
A123 True
C521 False
A224 True
B221 False
如需了解更多信息,请参阅:查询LINQ to DataSet [ ^ ]
Hi All,
I have a requirement to compare a text box input (Which would have a list of alphanumeric numbers separated by a delimiter) with a list of numbers in a dynamic datatable. The result on positive comparison is to show which ones among the list of alphanumeric numbers are in the datatable in an alert pop-up box.
And show a different alert if none of the containers in the list match the criteria.
I am currently able to achieve this for one input string. I just need sample code to split the string, store in an array and compare it with datatable. Thanks.
public void Search_click(Object sender, EventArgs E)
{
string path = @"C:\TEMP\SearchString.xlsx";
string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
OleDbConnection objConn = new OleDbConnection(connStr);
try
{
objConn.Open();
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM MyRange", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData");
string strExcelInfo = "";
strInfoText = _hiddentextInfo.Value;
bool foundinExcel = false;
if (objDataset1.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < objDataset1.Tables[0].Rows.Count; i++)
{
strExcelInfo = Convert.ToString(objDataset1.Tables[0].Rows[i]["Sheet1"].ToString());
if (strInfoText.Contains(strExcelInfo))
{
foundinExcel = true;
break;
}
else
{
continue;
}
}
}
if (foundinExcel == true)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + strInfoText + " is in the excel file')", true);
}
else if (foundinExcel == false)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + strInfoText + " is not in the excel file.')", true);
}
}
One of the simplest way is to use Linq to DataSet[^].
void Main() { string myDelimitedText = "A123,C521,A224,B221,"; DataTable dt = CreateDataTable(); var dl = myDelimitedText.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries) .Select(x=>new { Alpha = x, IsInDt = dt.AsEnumerable().Any(y=>y.Field<string>("MyField")==x) }); foreach (var a in dl) { Console.WriteLine("{0} {1}", a.Alpha, a.IsInDt); } } // Define other methods and classes here public static DataTable CreateDataTable() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("MyField", Type.GetType("System.String"))); dt.Rows.Add(new Object[]{"A123"}); dt.Rows.Add(new Object[]{"B123"}); dt.Rows.Add(new Object[]{"C123"}); dt.Rows.Add(new Object[]{"A224"}); dt.Rows.Add(new Object[]{"C324"}); return dt; }
Result:
Alpha IsInDt A123 True C521 False A224 True B221 False
For futher information, please see: Queries in LINQ to DataSet[^]
这篇关于将字符串值列表与数据表进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!