0
我有迭代列表的代碼。如何過濾這個linq查詢中的空值?
public static AffiliateList FromDate(string date)
{
Console.WriteLine("calling affiliates by date for " + date);
AffiliateList al;
try
{
al = new AffiliateList(DirectTrackXmlUtility.AffilaitesByDate(date));
}
catch (Exception)
{
return null;
}
return al;
}
public override IEnumerator<AffiliateItem> GetEnumerator()
{
return (from ru in Inner.resourceURL
select ru.location
into date select FromDate(date)
into listForDate from ru2 in listForDate.Inner.resourceURL
select AffiliateItem.From(ru2)).
GetEnumerator();
}
問題:如何修改處理FromDate返回null的代碼?
注意:我永遠都不會想出如何編寫這個查詢,但是我的嵌套foreach循環爲我重新做了更清晰的工作,現在我很感興趣,看看我能否使它工作...
更新:這是最終的linq查詢,以及最初的non-linq方式註釋掉。
return (from ru in Inner.resourceURL
select ru.location
into date select FromDate(date)
into listForDate where listForDate != null from ru2 in listForDate.Inner.resourceURL
select AffiliateItem.From(ru2)).GetEnumerator();
//foreach (resourceListResourceURL ru in Inner.resourceURL)
//{
// string date = ru.location;
// AffiliateList listForDate = FromDate(date);
// if (listForDate != null)
// {
// foreach (var ru2 in listForDate.Inner.resourceURL)
// {
// yield return AffiliateItem.From(ru2);
// }
// }
//}
你能告訴我foreach循環嗎?我認爲創建一個LINQ查詢比從那個...更容易。 – Femaref 2011-03-31 22:15:50
您想如何處理FromDate爲空?跳過它?使用默認日期? – 2011-03-31 22:17:08
難道你不能使用'using'代替try/catch嗎? – 2011-03-31 22:19:43