Using multiple data items in foreach loop

maliks

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?

InBetween

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_IDs. 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.

edited at
0

Comments

0 comments
Login to comment

Related

How to add Json items using forEach loop?

Deleting multiple records in laravel using foreach loop

Foreach loop using multiple str_replace

fpdf generate multiple pages using a foreach loop

multiple curves in foreach loop using Zedgraph

List<Model> Copies same data in list items in foreach loop

How to foreach loop data in Controller using addonisjs

Gathering Data For ClosedXML Using foreach Loop

using a Modal to view data in @foreach loop

Storing Individual Data Using a ForEach Loop Scala

Show JSON Data in a Table using foreach loop

random loop with jquery on Json data using forEach

Show Multiple images using foreach loop using Glide

Javascript foreach loop on list items

Grouping items in PHP foreach loop

foreach powershell loop open items

php foreach loop duplicate items

Loop over API data and display multiple search items in the DOM with JavaScript

Generating calendar using php Carbon in Laravel showing multiple dates while going though foreach loop for adding extarenal data

How to loop through multiple foreach loops and insert data into the database

How to return data in foreach loop having multiple async functions inside

Multiple sheets for ForEach Loop

Codeigniter multiple foreach loop

Multiple Foreach Loop Codeigniter

fetching all sub-list items under each row using foreach loop inside while loop

How to fetch multiple items from object using for loop

Nested foreach loop (3 items + 1 / loop)

multiple image upload using foreach loop and inserting to database

EF - Update multiple rows in database without using foreach loop