Reputation: 17186
I have code that iterates over a list of lists.
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();
}
Question: How do I modify the code to handle FromDate returning null?
Note: I would never have figured out how to write this query, but re-sharper did it for me from my nested foreach loops, and now I'm interested to see if I can make it work...
Update: Here's the final linq query, along with the original non-linq way commented out.
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);
// }
// }
//}
Upvotes: 0
Views: 202
Reputation: 32576
How about
int date select (FromDate(date) ?? new AffiliateList())
or
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)
Upvotes: 1
Reputation: 61437
return Inner.resourceURL.Select(ru => FromDate(ru.location))
.Where(d => d != null)
.SelectMany(ru =>
ru.Inner.resourceURL
.Select(ru2 => AffiliateItem.From(ru2)))
Forgive me for not using the query syntax, I just find the method chain much more expressive.
Also note that this was coded without any IDE so there is a possibility of error.
Upvotes: 0