如何检查值为null的对象列表?

时间:2017-05-05 03:55:41

标签: c#

我在变量 [boto] ERROR: Caught exception reading instance data Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/boto/utils.py", line 210, in retry_url r = opener.open(req, timeout=timeout) File "/usr/lib/python2.7/urllib2.py", line 429, in open response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py", line 447, in _open '_open', req) File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open return self.do_open(httplib.HTTPConnection, req) File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open raise URLError(err) URLError: <urlopen error [Errno 101] Network is unreachable> [boto] ERROR: Unable to read instance data, giving up [scrapy] ERROR: Error downloading <GET https://www.timeanddate.com/worldclock/> Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/scrapy/utils/defer.py", line 45, in mustbe_deferred result = f(*args, **kw) File "/usr/lib/python2.7/dist- packages/scrapy/core/downloader/handlers/__init__.py", line 41, in download_request return handler(request, spider) File "/usr/lib/python2.7/dist- packages/scrapy/core/downloader/handlers/http11.py", line 44, in download_request return agent.download_request(request) d = super(CachingThreadedResolver, self).getHostByName(name, timeout) File "/home/priyanka/.local/lib/python2.7/site- packages/twisted/internet/base.py", line 276, in getHostByName timeoutDelay = sum(timeout) TypeError: 'float' object is not iterable [scrapy] INFO: Dumping Scrapy stats: {'downloader/exception_count': 1, 'downloader/exception_type_count/exceptions.TypeError': 1, 'downloader/request_bytes': 228, 'log_count/DEBUG': 2, 'log_count/ERROR': 3, 'log_count/INFO': 7, 'scheduler/dequeued': 1, 'scheduler/dequeued/memory': 1, 中有一个对象列表,只有一个对象的值为null,我试图将此条件验证为SanityResults但是失败了?如何检查这种情况?

enter image description here

if (SanityResults != null)

4 个答案:

答案 0 :(得分:1)

您正在使用的条件将检查SanityResults是否为空。但是您想检查列表中所有对象的属性。所以更好的选择是使用Any()如果你想检查列表中的任何对象是否为null意味着你必须使用如下所示:

if(SanityResults.Any(x => x == null))
{
   // This will execute if any one of the object in the list is null
}

现在试试这个,如果你想检查列表中每个对象的属性:

if(SanityResults.Any(x => x.failCount==null || x.htmllist ==null))
{
    // include conditions like this for all required properties
    // this statement will execute if any of the property of any of the objects in the list is null
}

答案 1 :(得分:1)

为包含null

创建单独的方法
firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    profiler1:
        pattern: ^/_profiler
        security: false
        anonymous: true

    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    gos_websocket:
        pattern: ^/channel
        security: false

    oauth_token:
      pattern:    ^/oauth/v2/token
      security:   false

    api_doc:
      pattern:  ^/api/doc
      security: false

    oauth_authorize:
      pattern:    ^/oauth/v2/auth
      security:   false

    api:
      pattern:    ^/api
      fos_oauth:  true
      stateless:  true
      anonymous:  false

    main:
        pattern:    ^/
        #stateless: false
        #simple_preauth:
        #    authenticator: apikey_authenticator
        #provider: api_key_account_provider
        provider: ms.user_provider
        logout:
            path: /security/logout
            target: /security/login
        form_login:
            login_path: /security/login
            check_path: /security/check-login
            default_target_path: /dashboard
            always_use_default_target_path: true

   #anonymous: true

   # activate different ways to authenticate


   # http_basic: ~
   # http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate


   # form_login: ~
   # http://symfony.com/doc/current/cookbook/security/form_login_setup.html
access_control:
    #- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
    # - { path: ^/, roles: IS_AUTHENTICATED_FULLY }
    - { path: ^/security/login, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/, roles: IS_AUTHENTICATED_FULLY }

} }

答案 2 :(得分:1)

好的,只是把另一个类似的答案扔进戒指......

if (SanityResults == null || SanityResults.Any(sr => sr == null) || 
    SanityResults.Any(sr => sr.failcount == null && sr.htmllist == null &&
    sr.passcount == null && sr.testsuitename == null))
{
   // Do something if the List is null, if any items in the list are null, 
   // or all of the properties of any item in the list are null
}

答案 3 :(得分:0)

问题是SanityResults实际上不是null,并且由一个具有属性的元素组成,这些元素为null。

哪些元素可以为null,哪些不是?

如果没有允许任何属性为null,那么请使用:

if(SanityResults.Any(x => x.failCount == null || x.passcount == null || x.testsuitename == null || x.htmllist == null))
{
    // Code goes here
}

列表中的一个元素,除了其中只有空值,但在代码中有一个语义值,但有点气味。