访问 List<> 中字符串数组中的元素
Access Elements which are in a string-array within a List<>
我有几个字符串数组,都采用这种方案:
string[0] = article number;
string[1] = description;
string[2] = amount;
现在,列表包含大约 50 个这样的数组。
现在我想知道如何访问数组中的这些值。
例如:
我在文本框中输入商品编号。
现在应该搜索数组以在其“0”索引中包含文章编号。
我该如何实现?
我试过类似的东西:
for(int i = 0; i<List.length;i++)
{
if(List[i[0]] == txtBox.Text;
{
doSomething();
break;
}
}
当然这还不是很好。
我建议你这样定义 class
public class Article
{
public string ArticleNumber { get; set;}
public string Description { get; set; }
public string Amount { get; set; }
}
其目的是保存有关文章的所有信息,现在存储在一个数组中,这不是最好的方法。
那么你应该创建一个 arcticles 列表:
var articles = new List<Article>();
您将在其中添加文章。
这样做,你想要的是:
// This would check if there is any article in your list, whose article
// number starts with the letters in the txtBox.Text
if(articles.Any(article=>article.ArticleNumber.Contains(txtBox.Text))
DoSomething();
// If you want to search if there is any article, whose article number
// matches the inserted number in the textbox, then you have to change the
// above
if(articles.Any(article=>article.ArticleNumber == txtBox.Text))
DoSomething();
如果您的目的是使用可能找到的存在的文章,那么我们应该将上面的内容更改为以下内容:
var article = articles.FirstOrDefault(article=>
article.ArticleNumber.Contains(txtBox.Text));
或
var article = articles.FirstOrDefault(article=>
article.ArticleNumber == txtBox.Text);
if(article!=null)
DoSomething();
此版本与第一个版本之间的区别在于,您知道如果找到 article
就可以使用它,而无需再次查询您的序列。
您现有的代码有几个问题:
- 要访问另一个索引中的索引,[] 需要在彼此之后而不是在内部。即 [0][0] 而不是 [0[0]]
- 因为您没有任何 {} 用于 if 语句,循环将在第一次迭代后中断。
试试把它改成这样
for(int i = 0; i<List.length;i++)
{
if(List[i][0] == txtBox.Text)
{
doSomething();
break;
}
}
正如其他评论所指出的那样,最好使用 class 和 linq 来解决这个问题
public class MyClass
{
public string ArticleNumber {get; set;}
public string Description {get; set;}
}
使用linq搜索一下
var list = new List<MyClass>()
if (list.Any(i => i.ArticleNumber.Equals(txtBox.Text)))
{
DoSomething();
}
我有几个字符串数组,都采用这种方案:
string[0] = article number;
string[1] = description;
string[2] = amount;
现在,列表包含大约 50 个这样的数组。 现在我想知道如何访问数组中的这些值。
例如: 我在文本框中输入商品编号。 现在应该搜索数组以在其“0”索引中包含文章编号。 我该如何实现? 我试过类似的东西:
for(int i = 0; i<List.length;i++)
{
if(List[i[0]] == txtBox.Text;
{
doSomething();
break;
}
}
当然这还不是很好。
我建议你这样定义 class
public class Article
{
public string ArticleNumber { get; set;}
public string Description { get; set; }
public string Amount { get; set; }
}
其目的是保存有关文章的所有信息,现在存储在一个数组中,这不是最好的方法。
那么你应该创建一个 arcticles 列表:
var articles = new List<Article>();
您将在其中添加文章。
这样做,你想要的是:
// This would check if there is any article in your list, whose article
// number starts with the letters in the txtBox.Text
if(articles.Any(article=>article.ArticleNumber.Contains(txtBox.Text))
DoSomething();
// If you want to search if there is any article, whose article number
// matches the inserted number in the textbox, then you have to change the
// above
if(articles.Any(article=>article.ArticleNumber == txtBox.Text))
DoSomething();
如果您的目的是使用可能找到的存在的文章,那么我们应该将上面的内容更改为以下内容:
var article = articles.FirstOrDefault(article=>
article.ArticleNumber.Contains(txtBox.Text));
或
var article = articles.FirstOrDefault(article=>
article.ArticleNumber == txtBox.Text);
if(article!=null)
DoSomething();
此版本与第一个版本之间的区别在于,您知道如果找到 article
就可以使用它,而无需再次查询您的序列。
您现有的代码有几个问题:
- 要访问另一个索引中的索引,[] 需要在彼此之后而不是在内部。即 [0][0] 而不是 [0[0]]
- 因为您没有任何 {} 用于 if 语句,循环将在第一次迭代后中断。
试试把它改成这样
for(int i = 0; i<List.length;i++)
{
if(List[i][0] == txtBox.Text)
{
doSomething();
break;
}
}
正如其他评论所指出的那样,最好使用 class 和 linq 来解决这个问题
public class MyClass
{
public string ArticleNumber {get; set;}
public string Description {get; set;}
}
使用linq搜索一下
var list = new List<MyClass>()
if (list.Any(i => i.ArticleNumber.Equals(txtBox.Text)))
{
DoSomething();
}