使用Array.FindAll()迭代数组

时间:2016-04-21 15:09:29

标签: c# arrays

我正在尝试根据日期返回数组中的记录子集。

我的代码如下所示:

<!ELEMENT dblp (article|inproceedings|proceedings|book|incollection|
                phdthesis|mastersthesis|www)*>
<!ENTITY % field "author|editor|title|booktitle|pages|year|address|journal|volume|number|month|url|ee|cdrom|cite|publisher|note|crossref|isbn|series|school|chapter">

<!ELEMENT article       (%field;)*>
<!ATTLIST article
                        key CDATA #REQUIRED
                        mdate CDATA #IMPLIED
                        publtype CDATA #IMPLIED
                        reviewid CDATA #IMPLIED
                        rating CDATA #IMPLIED
>

<!ELEMENT inproceedings (%field;)*>
<!ATTLIST inproceedings key CDATA #REQUIRED
                        mdate CDATA #IMPLIED
                        publtype CDATA #IMPLIED
>

<!ELEMENT proceedings   (%field;)*>
<!ATTLIST proceedings   key CDATA #REQUIRED
                        mdate CDATA #IMPLIED
                        publtype CDATA #IMPLIED
>

<!ELEMENT book          (%field;)*>
<!ATTLIST book          key CDATA #REQUIRED
                        mdate CDATA #IMPLIED
                        publtype CDATA #IMPLIED
>

<!ELEMENT incollection  (%field;)*>
<!ATTLIST incollection  key CDATA #REQUIRED
                        mdate CDATA #IMPLIED
                        publtype CDATA #IMPLIED
>

<!ELEMENT phdthesis     (%field;)*>
<!ATTLIST phdthesis     key CDATA #REQUIRED
                        mdate CDATA #IMPLIED
                        publtype CDATA #IMPLIED
>

<!ELEMENT mastersthesis (%field;)*>
<!ATTLIST mastersthesis key CDATA #REQUIRED
                        mdate CDATA #IMPLIED
                        publtype CDATA #IMPLIED
>

<!ELEMENT www           (%field;)*>
<!ATTLIST www           key CDATA #REQUIRED
                        mdate CDATA #IMPLIED
                        publtype CDATA #IMPLIED
>

<!ELEMENT author    (#PCDATA)>
<!ATTLIST author
                    aux CDATA #IMPLIED
                    bibtex CDATA #IMPLIED
>
<!ELEMENT editor    (#PCDATA)>
<!ATTLIST editor
                    aux CDATA #IMPLIED
>
<!ELEMENT address   (#PCDATA)>

<!ENTITY % titlecontents "#PCDATA|sub|sup|i|tt|ref">
<!ELEMENT title     (%titlecontents;)*>
<!ATTLIST title
                    bibtex CDATA #IMPLIED
>
<!ELEMENT booktitle (#PCDATA)>
<!ELEMENT pages     (#PCDATA)>
<!ELEMENT year      (#PCDATA)>
<!ELEMENT journal   (#PCDATA)>
<!ELEMENT volume    (#PCDATA)>
<!ELEMENT number    (#PCDATA)>
<!ELEMENT month     (#PCDATA)>
<!ELEMENT url       (#PCDATA)>
<!ELEMENT ee        (#PCDATA)>
<!ELEMENT cite      (#PCDATA)>
<!ELEMENT school    (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ATTLIST publisher
                    href CDATA #IMPLIED
>
<!ELEMENT note      (#PCDATA)>
<!ATTLIST note
                    type CDATA #IMPLIED
>
<!ELEMENT cdrom     (#PCDATA)>
<!ATTLIST cite
                    label CDATA #IMPLIED
                    ref CDATA #IMPLIED
>
<!ELEMENT crossref  (#PCDATA)>
<!ELEMENT isbn      (#PCDATA)>
<!ELEMENT chapter   (#PCDATA)>
<!ELEMENT series    (#PCDATA)>
<!ATTLIST series
                    href CDATA #IMPLIED
>
<!ELEMENT layout    ANY>
<!ATTLIST layout
                    logo CDATA #IMPLIED
>

<!ELEMENT ref (#PCDATA)>
<!ATTLIST ref href CDATA #REQUIRED>
<!ELEMENT sup (%titlecontents;)*>
<!ELEMENT sub (%titlecontents;)*>
<!ELEMENT i   (%titlecontents;)*>
<!ELEMENT tt  (%titlecontents;)*>

<!ENTITY reg   "&#174;">
<!ENTITY micro "&#181;">
<!ENTITY times "&#215;">

<!ENTITY Agrave  "&#192;" ><!-- capital A, grave accent -->
<!ENTITY Aacute  "&#193;" ><!-- capital A, acute accent -->
<!ENTITY Acirc   "&#194;" ><!-- capital A, circumflex accent -->
<!ENTITY Atilde  "&#195;" ><!-- capital A, tilde -->
<!ENTITY Auml    "&#196;" ><!-- capital A, dieresis or umlaut mark -->
<!ENTITY Aring   "&#197;" ><!-- capital A, ring -->
<!ENTITY AElig   "&#198;" ><!-- capital AE diphthong (ligature) -->
<!ENTITY Ccedil  "&#199;" ><!-- capital C, cedilla -->
<!ENTITY Egrave  "&#200;" ><!-- capital E, grave accent -->
<!ENTITY Eacute  "&#201;" ><!-- capital E, acute accent -->
<!ENTITY Ecirc   "&#202;" ><!-- capital E, circumflex accent -->
<!ENTITY Euml    "&#203;" ><!-- capital E, dieresis or umlaut mark -->
<!ENTITY Igrave  "&#204;" ><!-- capital I, grave accent -->
<!ENTITY Iacute  "&#205;" ><!-- capital I, acute accent -->
<!ENTITY Icirc   "&#206;" ><!-- capital I, circumflex accent -->
<!ENTITY Iuml    "&#207;" ><!-- capital I, dieresis or umlaut mark -->
<!ENTITY ETH     "&#208;" ><!-- capital Eth, Icelandic -->
<!ENTITY Ntilde  "&#209;" ><!-- capital N, tilde -->
<!ENTITY Ograve  "&#210;" ><!-- capital O, grave accent -->
<!ENTITY Oacute  "&#211;" ><!-- capital O, acute accent -->
<!ENTITY Ocirc   "&#212;" ><!-- capital O, circumflex accent -->
<!ENTITY Otilde  "&#213;" ><!-- capital O, tilde -->
<!ENTITY Ouml    "&#214;" ><!-- capital O, dieresis or umlaut mark -->
<!ENTITY Oslash  "&#216;" ><!-- capital O, slash -->
<!ENTITY Ugrave  "&#217;" ><!-- capital U, grave accent -->
<!ENTITY Uacute  "&#218;" ><!-- capital U, acute accent -->
<!ENTITY Ucirc   "&#219;" ><!-- capital U, circumflex accent -->
<!ENTITY Uuml    "&#220;" ><!-- capital U, dieresis or umlaut mark -->
<!ENTITY Yacute  "&#221;" ><!-- capital Y, acute accent -->
<!ENTITY THORN   "&#222;" ><!-- capital THORN, Icelandic -->
<!ENTITY szlig   "&#223;" ><!-- small sharp s, German (sz ligature) -->
<!ENTITY agrave  "&#224;" ><!-- small a, grave accent -->   
<!ENTITY aacute  "&#225;" ><!-- small a, acute accent -->
<!ENTITY acirc   "&#226;" ><!-- small a, circumflex accent -->
<!ENTITY atilde  "&#227;" ><!-- small a, tilde -->
<!ENTITY auml    "&#228;" ><!-- small a, dieresis or umlaut mark -->
<!ENTITY aring   "&#229;" ><!-- small a, ring -->
<!ENTITY aelig   "&#230;" ><!-- small ae diphthong (ligature) -->
<!ENTITY ccedil  "&#231;" ><!-- small c, cedilla -->
<!ENTITY egrave  "&#232;" ><!-- small e, grave accent -->
<!ENTITY eacute  "&#233;" ><!-- small e, acute accent -->
<!ENTITY ecirc   "&#234;" ><!-- small e, circumflex accent -->
<!ENTITY euml    "&#235;" ><!-- small e, dieresis or umlaut mark -->
<!ENTITY igrave  "&#236;" ><!-- small i, grave accent -->
<!ENTITY iacute  "&#237;" ><!-- small i, acute accent -->
<!ENTITY icirc   "&#238;" ><!-- small i, circumflex accent -->
<!ENTITY iuml    "&#239;" ><!-- small i, dieresis or umlaut mark -->
<!ENTITY eth     "&#240;" ><!-- small eth, Icelandic -->
<!ENTITY ntilde  "&#241;" ><!-- small n, tilde -->
<!ENTITY ograve  "&#242;" ><!-- small o, grave accent -->
<!ENTITY oacute  "&#243;" ><!-- small o, acute accent -->
<!ENTITY ocirc   "&#244;" ><!-- small o, circumflex accent -->
<!ENTITY otilde  "&#245;" ><!-- small o, tilde -->
<!ENTITY ouml    "&#246;" ><!-- small o, dieresis or umlaut mark -->
<!ENTITY oslash  "&#248;" ><!-- small o, slash -->
<!ENTITY ugrave  "&#249;" ><!-- small u, grave accent -->
<!ENTITY uacute  "&#250;" ><!-- small u, acute accent -->
<!ENTITY ucirc   "&#251;" ><!-- small u, circumflex accent -->
<!ENTITY uuml    "&#252;" ><!-- small u, dieresis or umlaut mark -->
<!ENTITY yacute  "&#253;" ><!-- small y, acute accent -->
<!ENTITY thorn   "&#254;" ><!-- small thorn, Icelandic -->
<!ENTITY yuml    "&#255;" ><!-- small y, dieresis or umlaut mark -->

Accounts数组有一个modification_date字段,但返回的是所有帐户。在我的测试样本中,2个具有mod日期&gt; 2016年5月15日。这种代码适用于其他领域,但不适用于以下日期:

var SomeAccounts = Array.FindAll(Accounts, x => x.modification_date > modification_date);

if (SomeAccounts == null)
{
    return NotFound();
}

return Ok(JsonConvert.SerializeObject(SomeAccounts));

我做错了什么?

1 个答案:

答案 0 :(得分:1)

确保您的数组实际上是空的

首先,Array.FindAll()方法永远不会实际返回null,因为如果找不到匹配项,它将返回一个空数组,因此您可能想要查看是否有任何值发现:

if (SomeAccounts.Length == 0)
{
    return NotFound();
}

如果你返回的结果是一个数组(没有值),那么这是正确的(因为它以前不会因为它永远不为空而点击NotFound()。)

审核日期

作为评论中提到的Cory,比较DateTime对象可能非常困难。您将需要确保在比较值时考虑TimeZones等任何信息。

您也可以考虑使用调试器手动检查DateTime对象在代码中的外观(modification_date属性和实际的modification_date变量)。尝试在Watch窗口中比较它们,以确保比较正常。