Two lists

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have two lists: "List<Article> articles" and "List<Document>
documents".
Both Article and Document contains a property named Updated.

How can I get the most recent Updated date considering all documents
and articles?

Thanks,
Miguel
 
Hello,

I have two lists: "List<Article> articles" and "List<Document>
documents".
Both Article and Document contains a property named Updated.

How can I get the most recent Updated date considering all documents
and articles?

Just find the most recent in each list so that you have a single Article
and a single Document, and then choose the most recent between those two.

Pete
 
shapper said:
Hello,

I have two lists: "List<Article> articles" and "List<Document>
documents".
Both Article and Document contains a property named Updated.

How can I get the most recent Updated date considering all documents
and articles?

Loop over all of the items of both kinds and take the most recent date.

For instance, without any fancy LINQ or anything, you can use a couple
of loops:

DateTime mostRecent = DateTime.MinValue;
foreach (Article art in articles)
{
if (art.Updated>mostRecent) mostRecent=art.Updated;
}
foreach (Document doc in documents)
{
if (doc.Updated>mostRecent) mostRecent=doc.Updated;
}
//Now , mostRecent contains the most recent Updated date, unless it equals
DateTime.MinValue whihc would mean that both lists were empty.
 
    Loop over all of the items of both kinds and take the most recentdate.

    For instance, without any fancy LINQ or anything, you can use a couple
of loops:

DateTime mostRecent = DateTime.MinValue;
foreach (Article art in articles)
{
    if (art.Updated>mostRecent) mostRecent=art.Updated;}

foreach (Document doc in documents)
{
    if (doc.Updated>mostRecent) mostRecent=doc.Updated;}

//Now , mostRecent contains the most recent Updated date, unless it equals
DateTime.MinValue whihc would mean that both lists were empty.

I was following this:

DateTime documentDate = documents.OrderBy(d => d.Created).First
().Created;
DateTime articleDate = articles.OrderBy(a => a.Created).First
().Created;

But my idea was more to create a new list<DateTime> by joining
articles and documents lists but only taking the Updated field, then
order by Updated and take first.

I am just not sure if this joining part on one field is possible
 
I was following this:

DateTime documentDate = documents.OrderBy(d =>
d.Created).First().Created;
DateTime articleDate = articles.OrderBy(a =>
a.Created).First().Created;

Why? Sorting is much less efficient than a single scan.
But my idea was more to create a new list<DateTime> by joining
articles and documents lists but only taking the Updated field, then
order by Updated and take first.

I am just not sure if this joining part on one field is possible

A join will create a new collection with the elements of the original
collections paired up with each other. It's not the same as concatenation.

You could, of course, use "select" to project each collection into a new
anonymous type ordered by the Updated field, but that would just cause
your sorting overhead to be even greater (it's O(n log n) so a doubling,
for example, of the number of elements results in _more_ than a doubling
of cost).

The best solution is the one that both Alberto and I described to you (you
shouldn't need someone to write the code for you, but he was nice and did
it anyway).

Pete
 
Back
Top