2011-05-19 30 views
6

我有一个从数据库导入记录的ArrayList。 是否有任何方法来检查arrayList是否包含schname,我想匹配另一个列表是api?检查包含特定字符串的数组列表的方法

List<PrimaryClass> primaryList = new List<PrimaryClass>(e.Result); 
PrimaryClass sc = new PrimaryClass(); 
foreach (string item in str) 
{ 
    for (int a = 0; a <= e.Result.Count - 1; a++) 
    { 
     string schname = e.Result.ElementAt(a).PrimarySchool; 
     string tophonour = e.Result.ElementAt(a).TopHonour; 
     string cca = e.Result.ElementAt(a).Cca; 
     string topstudent = e.Result.ElementAt(a).TopStudent; 
     string topaggregate = e.Result.ElementAt(a).TopAggregate; 
     string topimage = e.Result.ElementAt(a).TopImage;   
     if (item.Contains(schname)) 
     { 
     } 
    } 
} 

这是我到目前为止所提出的,善意纠正我可能犯下的任何错误。谢谢。

+4

我希望你*实际上*有一个'List '如果它是Silverlight的 - 我认为它不支持非泛型集合。请编辑您的问题以清除此问题,以及列表中的* actual *类型的数据。 – 2011-05-19 06:34:58

+0

如果您想获得高质量的答案,您应该添加更多信息和代码示例。 – 2011-05-19 06:47:36

+0

使用通用集合(现在是2011年,现在有一半!),它会帮助你很多 – abatishchev 2011-05-19 06:47:46

回答

5

试试这个

foreach(string row in arrayList){ 
    if(row.contains(searchString)){ 
     //put your code here. 
    } 
} 
2
// check all types 
var containsAnyMatch = arrayList.Cast<object>().Any(arg => arg.ToString() == searchText); 

// check strings only 
var containsStringMatch = arrayList.OfType<string>().Any(arg => arg == searchText); 
+0

Silvelight是否完全支持这样的LINQ? – abatishchev 2011-05-19 06:46:34

+0

@abatishchev - 是的,它的确如此 – 2011-05-19 06:50:21

4

好了,现在你已经表明,它实际上是一个List<T>,应该很容易使用LINQ:

if (primaryList.Any(x => item.Contains(x.PrimarySchool)) 

请注意,你真的应该考虑使用foreach而不是for循环遍历列表,除非你绝对需要索引...如果你正在处理一个列表,使用索引器比调用ElementAt简单。

+0

你会在哪里把schname放在这里,如果这就是我想要使用的字段 – GJJ 2011-05-19 09:49:02

+0

@GJJ:据我所见,'schname'不是'PrimaryClass'属性键入...你正在从'PrimarySchool'属性初始化*局部变量*(不是字段)'schname',这就是我使用它的原因。 – 2011-05-19 09:52:26

相关问题