所以我有一个物品对象列表。在装备中,我有15个获取和设置方法。我想要构造一个搜索方法来循环列表中的所有对象以及每个Materiel对象中的所有变量。循环部分很容易,但我正在努力与字符串包含部分。例如,搜索词可以是“acto”,我应该对“拖拉机”有所打击。我试过使用string-Contains类,但据我所知,它只检查从0开始的字符串。所以“Tra”得到一个命中,但不是“acto”。c#包含字符串的一部分
在类中是否有构建,或者我应该自己编程?
对不起,不好解释。
我的代码。我现在明白,我得到命中为子,还包括其他的结果:)
protected void Button_search_Click(object sender, EventArgs e)
{
string searchTerm = TextBox1.Text.ToString().ToLower();
TableRow row;
TableCell cell;
int rowNumber = 1;
foreach (Materiell mat in allItems)
{
if (searchTerm.Contains(mat.itemID.ToString().ToLower()) ||
searchTerm.Contains(mat.manufacturer.ToLower()) ||
searchTerm.Contains(mat.model.ToLower()) ||
searchTerm.Contains(mat.serialNo.ToLower()) ||
searchTerm.Contains(mat.dateProd.ToString().ToLower()) ||
searchTerm.Contains(mat.location.ToLower()) ||
searchTerm.Contains(mat.mainCategory.ToLower()) ||
searchTerm.Contains(mat.subCategory.ToLower()) ||
searchTerm.Contains(mat.dateAcquired.ToString().ToLower()) ||
searchTerm.Contains(mat.price.ToString().ToLower()) ||
searchTerm.Contains(mat.ownerID.ToString().ToLower()) ||
searchTerm.Contains(mat.extra.ToString().ToLower()) ||
searchTerm.Contains(mat.textComment.ToLower()) ||
searchTerm.Contains(mat.active.ToString().ToLower()))
{
row = new TableRow();
row.ID = "row" + rowNumber.ToString();
rowNumber++;
cell = new TableCell();
cell.Text = "<a href=\"#\" class=\"opendiv\">" + mat.itemID.ToString() + "</a>";
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.manufacturer.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.model.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.serialNo.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.dateProd.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.location.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.mainCategory.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.subCategory.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.dateAcquired.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.price.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.ownerID.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.extra.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.ownDefData.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.textComment.ToString();
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = mat.active.ToString();
row.Cells.Add(cell);
Table1.Rows.Add(row);
}
}
}
你可以张贴的代码片段,因为根据文档“雅图”应该打在“拖拉机”:http://msdn.microsoft.com/en-us/library/dy85x1sa(v=vs.100).aspx – LexyStardust 2012-03-29 10:06:24
'“拖拉机”.Contains(“acto”)'应该返回'true'。您可能想要发布一些代码,以便我们可以看到迄今为止尝试的内容以及可能出错的位置。 – Rawling 2012-03-29 10:06:28
Gah。没有一个答案能够解决这个问题。看看Lucene.NET,也许(你似乎在寻找全文搜索/索引;这可能是一个库类型的应用程序?) – sehe 2012-03-29 10:21:06