I have the data items as:
Dictionary<int, Tuple<int, int>> tPapers = eAuthor.GetPapersBetweenYears(year, year + 1);
List<int> tCoAuthors = eAuthor.GetCoAuthorsBetweenYears(year, year + 1);
List<int> tVenues = eAuthor.GetVenuesBetweenYears(year, year + 1);
I have to write all these data items i.e. tPapers
, tCoAuthors
and tVenues
into the text file. I tried as:
foreach (var kvpaper in tPapers)
{
// Key is Paper_ID, Item1 is Paper_Category, Item2 is Year
twObjClus.WriteLine("PaperID: {0}, PaperCategory: {1}, Year: {2}",
kvpaper.Key, kvpaper.Value.Item1, kvpaper.Value.Item2);
}
Whereas I want to have output written in this form as:
Paper_ID: 1, Paper_Category: 3, CoAutohr_ID: 34, Venue_ID: 23, Year: 2005
How can I use all these data items in a single foreach()
loop or separate loops needed for each of data items?
You do not have the tools to solve your problem as it stands right now. Your methods GetCoAuthorsBetweenYears
and GetVenuesBetweenYears
simply do not return the necessary information.
Why? Well, how do you relate any given record returned by GetCoAuthorsBetweenYears
to a Paper_Id
? This method simply returns all CoAuthors
stored in eAuthors
. What you need is a method with one of the following signatures:
List<Tuple<int, int>> GetDocumentsAndCoAuthorsBetweenYears(int lower, int upper) //Tuple.Value1 stores Paper_Id and Tuple.Value2 stores CoAuthor_ID.
List<int> GetCoAuthorsByDocumentBetweenYears(int documentId, int lower, int upper)
Now, in both cases you can relate CoAuthor_ID
information with Paper_ID
s. Same with venues.
Ok, but if you can modify these methods, then why are we doing this to begin with? What prevents you from implementing the following one:
IEnumerable<PaperInfo> GetPaperInfoBetweenYears(int lower, int upper);
Where PaperInfo
would be:
class PaperInfo
{
public int Paper_ID { get; set; }
public int CoAuthor_ID { get; set; }
public int Paper_Category { get; set; }
public int Venue_ID { get; set; }
}
And now you'd simply print your IEnumerable<PaperInfo>
:
var papers = GetAllPaperInfoBetweenYears(year, year + 1);
var printedInfo = string.Join(Environment.NewLine,
papers.Select(p => string.Format("{0} {1} {2} {3}", p.Paper_ID, p.Paper_Category, p.CoAuthor_ID, p.Venue_ID));
UPDATE Per your comments, I've put together a small example:
public class Author
{
public int Paper_ID { get; set; }
public int CoAuthor_ID { get; set; }
public int Venue_ID { get; set; }
public int Paper_Category { get; set; }
public int Year { get; }
public int Publisher_ID { get; }
//etc.
}
//bring all info from database
IEnumerable<Author> eAuthors = GetAllInfoFromDB();
//Now filter and project what you need
public static IEnumerable<PaperInfo> GetGetPaperInfoBetweenYears(this List<Author> eAuthors, int lower, int upper)
{
return from eAuthor in eAuthors
where (eAuthor.Year >= lower && eAuthor.Year < upper)
select new PaperInfo() { Paper_ID = eAuthor.Paper_ID, CoAuthor_ID = eAuthor.CoAuthor_ID, Paper_Category = eAuthor.Paper_Category, Venue_ID = eAuthor.Venue_ID };
}
Of course you could even do without PaperInfo
and simply filter by year and project the whole information stored in eAuthorInfo
:
public static IEnumerable<PaperInfo> GetGetPaperInfoBetweenYears(this List<Author> eAuthors, int lower, int upper)
{
return from eAuthor in eAuthors
where (eAuthor.Year >= lower && eAuthor.Year < upper)
select eAuthor;
}
And, same as before, simply print out the info you need:
var printedInfo = string.Join(Environment.NewLine,
papers.Select(p => string.Format("{0} {1} {2} {3}", p.Paper_ID, p.Paper_Category, p.CoAuthor_ID, p.Venue_ID)); //no year, publisher, etc. info
This is the way I'd do it, projections are really useful but are a pain when you have a lot of them and you can't use anonymous types; it makes you have to implement a type for each projection.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments