Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#在列表对象中查找对象_C# - Fatal编程技术网

C#在列表对象中查找对象

C#在列表对象中查找对象,c#,C#,我想做的是,在列表中找到一个对象 Log q = logs.Find(x => (x.dnsq.queryID.Contains(queryid))); 然而,当我这样做时,我得到了这个错误 x、 dnsq.queryID='x.dnsq.queryID'引发了类型为的异常 'System.NullReferenceException Log类如下所示 class Log : LogCtrl { public int id { set; get; } public str

我想做的是,在列表中找到一个对象

Log q = logs.Find(x => (x.dnsq.queryID.Contains(queryid)));
然而,当我这样做时,我得到了这个错误

x、 dnsq.queryID='x.dnsq.queryID'引发了类型为的异常 'System.NullReferenceException

Log类如下所示

class Log : LogCtrl
{
    public int id { set; get; }
    public string source { set; get; }
    public string destination { set; get; }
    public string protocol { set; get; }
    public double time { set; get; }
    public string info { set; get; }
    public DNSresponse dnsr { set; get; }
    public DNSquery dnsq { set; get; }
}

听起来x或x.dnsq或x.dnsq.queryId对于日志中至少一项为空。您可以通过执行以下操作在声明中检查这一点:

    Log q = logs.Find(x => (x != null && x.dnsq != null && x.dnsq.queryID != null && x.dnsq.queryID.Contains(queryid)));

您的一个对象为null或dnsq为null或queryID为null或带有新的
?。
运算符。我使用调试器检查日志列表,我知道它包含一个条目,其中dnsq.queryID等于我要搜索的内容。@Mathias_uu不管列表是否包含它,只要其中一个字段为null,它就会抛出null exceptionGreat,这似乎没有诀窍,非常感谢!