(I have rewritten the question to try to better provide an example of what I am trying to do. I apologize if this is a confusing question still…)
In what way would it be possible to find the union of n-many sets of objects that are contained as properties of other objects?
Using the following as an example:
class Author
{
public string FirstName;
public string LastName;
}
class Magazine
{
public string Title;
public string ISBN;
public List<Article> TOC;
}
class Article
{
public string Title;
public string Header;
public string Body;
public int PageNumber;
public Magazine Source;
public Author Author;
}
- I am given a
List<Magazine>which contains aList<Article>. - The Article object has a Magazine and Author objects as properties (the article may exist 1:N to the magazine – as a copy or reprint).
- In fact, I am treating each article as an independent object. I do not care about the
List<Magazine>which was given to me. I have been asked to provide aList<Author>. - Before being given this List, I have NO knowledge of the possible Articles or Authors.
This data which I am provided looks like this (in treeview layout).
Magazine01
Article01 (Title, Header, ... Magazine01, Author01)
Article02 (Title, Header, ... Magazine01, Author02)
Article03 (Title, Header, ... Magazine01, Author03)
Magazine02
Article04 (Title, Header, ... Magazine02, Author04)
Article05 (Title, Header, ... Magazine02, Author05)
Article06 (Title, Header, ... Magazine02, Author04)
Magazine03
Article07 (Title, Header, ... Magazine03, Author03)
Article08 (Title, Header, ... Magazine03, Author02)
Article09 (Title, Header, ... Magazine03, Author01)
This data which I have been asked to provide looks like this (in treeview layout).
Author01
Article01 (Title, Header, ... Magazine01, Author01)
Article09 (Title, Header, ... Magazine03, Author01)
Author02
Article02 (Title, Header, ... Magazine01, Author02)
Article08 (Title, Header, ... Magazine03, Author02)
Author03
Article03 (Title, Header, ... Magazine01, Author03)
Article07 (Title, Header, ... Magazine03, Author03)
Author04
Article04 (Title, Header, ... Magazine02, Author04)
Article06 (Title, Header, ... Magazine02, Author04)
Author05
Article05 (Title, Header, ... Magazine02, Author05)
- The
List<Magazine>I am given will be in no particular order. - I am not trying to sort the output yet, but that would be nice.
- There may be n-many Magazines where each issue has n-many Articles.
- As a result, an Author may have n-many Articles.
- As I receive the Magazine objects, I am aggregating the Articles into a
List<Article>(but I am not dead set on continuing to do this, it seemed like a good idea…)
In what way can I reduce the looping through the list of articles other than my (shameful) “reduction by attrition” method below:
List<Magazine> magazineList; // Magazines contain a list of Articles which contain an Author.
List<Article> articleList; // As each Magazine is created, a summary list of the Articles is recorded here (I realize that this is probably unnecessary, but that is an aside to my problem)
Bag<Author> authorBag;
for (i = 0; i < (Magazines.Count - 1); i++)
{
BuildAuthorBag();
}
ParseArticleList();
//...
BuildAuthorBag()
{
// Finds occurrences of Author in the Article list of each Magazine.
// Limits the inclusion of Author based on preferences ( i > 1, 2, etc. )
}
ParseArticleList()
{
// I clone the articleList (i.e. tmpArticleList)
// I create two lists of Article type. One will be the new leaf nodes. The second is a swap list.
// Using the authorBag...
// In a loop...
// I create the new parent node from the current Author in the bag.
// Then, find occurrences of the Author in the tmpArticleList adding that Article as a child node to the parent node.
// or...
// I place them into the second list to be swapped for the tmpArticleList before the next loop.
}
From what I could understand, you need something like this. (I’m using VS2010 and C# 4, but any version that supports LINQ should do).
And this is what I’m seeing on screen, when I ran it.