如何在c#中搜索对象列表?

时间:2013-01-16 19:06:18

标签: c# linq collections

我还在学习C#,我正在尝试构建一个NIC列表,我稍后可以在我的代码中引用它来执行misc。功能。无论如何,我能够很好地填充列表,但现在的问题是我不确定以最佳方式在列表中搜索具有特定条件的NIC对象。我可以做一个foreach循环工作,但我不确定这是否是最好的方法。我搜索了这个主题并找到了一些关于如何使用LINQ或使用Lambdas进行高级搜索的材料,但对于开始编程的这些工作并没有真正的任何好的信息。

这是我创建对象和列表的代码以及我想要完成的伪代码:

//Constructs a NIC structure and a list to store NICs (my actual code)
struct NIC
{
    public string nicName;
    public string nicIp;
    public string nicGateway;
    public string nicMask;
}
List<NIC> nicList = new List<NIC>();




//Example searches in semi pseudocode
if nicList contains any NIC where anyNIC.nicIp first three chars .Contains("169")
{
//Do something
}


if nicList contains any NIC where anyNIC.nicIP != null 
{
//Do something
}

-Thanks

4 个答案:

答案 0 :(得分:2)

LINQ即将成为你最好的朋友。在这种情况下,我会使用Enumerable.Any方法。 Enumerable.AnyIEnumerable<T>上的扩展程序,因此您可以直接在nicList上调用它。您传递一个函数,该函数接受NIC的实例并返回truefalse

以下是您在特定情况下的样子:

if (nicList.Any(x => x.nicIp.StartsWith("169"))
{
   // Do something
}


if (nicList.Any(x => !string.IsNullOrEmpty(x.nicIP))
{
   // Do something
}

在这种情况下,如果您有一个或多个满足条件的元素,Enumerable.Any只返回true。如果要访问满足条件的元素,请使用LINQ方法Enumerable.Where。签名是相同的,但它不是bool,而是返回IEnumerable<T>

示例:

var nicsWithIp = nicList.Where(x => !string.IsNullOrEmpty(x.nicIP);

有关详细信息,请查看此MSDN页面:“Getting Started with LINQ in C#”。

答案 1 :(得分:0)

使用LINQ:

var nics = nicList.Where(n => n.nicIp.Substring(0, 3) == "169").ToList();

答案 2 :(得分:0)

怎么样:

if(nicList.Where(n => n.nicIp.Substring(0, 3) == "169").Any())
{

}

if(nicList.Where(n => n.nicIp != null).Any())
{

}

向第一个添加空检查也可能是明智的,以防止NullReferenceException:

if(nicList.Where(n => n.nicIp != null && n.nicIp.Substring(0, 3) == "169").Any())
{

}

答案 3 :(得分:0)

使用LINQ框架可以有几种方法。

您可以使用内嵌查询语法:

// Get all NICs with 169.x.x.x IP address
var nic169 = from nic in nicList
              where nic.nicIp != null && nic.nicIp.StartsWith("169")
              select nic;

var contains169Ip = nic169.Count() > 0;

// Get all NICs with non-null, non-empty IP address    
var validIpNics = from nic in nicList
                   where !string.IsNullOrWhiteSpace(nic.nicIp)
                   select nic;

或者,您可以将 lambda语法与LINQ方法一起使用:

// Do we have a 169.x.x.x NIC present?
var has169 = nicList.Any(nic => nic.nicIp != null && nic.nicIp.StartWith("169"));

// First NIC with a non-null, non-empty IP address
var firstNic = nicList.First(nic => !string.IsNullOrWhiteSpace(nic.nicIp));

为了更好地解释 lambda 表达式,请使用delegate类型的Func<string,bool>。这意味着“一个接收字符串且返回 bool值”的方法。此方法在.Any()方法中用作选择器,以确定任何对象是否匹配。

var func = (text) => { return !string.IsNullOrWhiteSpace(text); }

直接相当于:

bool OurLambdaFunc(string text) { return !string.IsNullOrWhiteSpace(text); }

.Any<T>(Func<T,bool> selector)方法扩展为类似的内容:

public bool Any<T>(IEnumerable<T> collection, Func<T, bool> selector)
{
  foreach(var value in collection)
    if(selector(value))
      return true;  // Something matched the selector method's criteria.

  // Nothing matched our selector method's criteria.
  return false;
}