Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the following C# code, how to add data from var aways to third column "Away" ???? With "return names.Zip ...." I can't....

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HtmlAgilityPack;

namespace GetGamerankingsData {
    public partial class Form1 : Form
    {
        DataTable table = new DataTable("GameRankingsDataView");
      

        HtmlWeb web = new HtmlWeb();
        

        public Form1()
            
        {


            InitializeComponent();
            InitTable();
        }
        public class NameAndScore {
    public string Name { get; set; }
    public string Score { get; set; }
      
            
        }

  
        private void InitTable()
        {
            
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Score", typeof(string));

           table.Columns.Add("Away", typeof(string));
          
  gameRankingsDataView.DataSource = table;
           
        }
        private async Task<List<nameandscore>> GameRankingsFromPage(int pageNum) {
            string url = "https://www.totalcorner.com/team/view/6";
            if (pageNum != 0)
                url = "https://www.totalcorner.com/team/view/6/page:" + pageNum.ToString();
            var doc = await Task.Factory.StartNew(() => web.Load(url));
            var nameNodes = doc.DocumentNode.SelectNodes("//*[@id='inplay_match_table']/tbody[3]/tr/td[1]/div/a");

            var scoreNodes = doc.DocumentNode.SelectNodes("//*[@id='inplay_match_table']/tbody[3]/tr/td[4]/a/span");
            var awayNodes = doc.DocumentNode.SelectNodes("//*[@id='inplay_match_table']/tbody[3]/tr/td[6]/a/span");

            if (nameNodes == null || scoreNodes == null)
                return new List<nameandscore>();

            var names = nameNodes.Select(node => node.InnerText);
            var scores = scoreNodes.Select(node => node.InnerText);
           
            var aways = scoreNodes.Select(node => node.InnerText);
            
            return names.Zip(scores, (name, score) => new NameAndScore() { Name = name, Score = score }).ToList();
        }
        private async void Form1_Load(object sender, EventArgs e) {
            int pageNum = 0;
            var rankings = await GameRankingsFromPage(0);
            while (rankings.Count > 0 && pageNum < 3)
            {
                foreach (var ranking in rankings)
                    
               table.Rows.Add(ranking.Name, ranking.Score);
                
                
                rankings = await GameRankingsFromPage(++pageNum);
            }
        }

    }
}
Posted
Updated 2-Feb-19 18:13pm
v4
Comments
ZurdoDev 29-Nov-18 15:27pm    
Please only post the relevant code. As is, I have no idea what you are asking or where you are stuck.
DENDRINOS K 29-Nov-18 16:31pm    
Thank you for your response ZurdoDev......
The part of my code :
return names.Zip(scores, (name, score) => new NameAndScore() { Name = name, Score = score }).ToList();

return only two of three values-arrays ToList for table (names and scores)...
I want to return and third (aways) .....
Sinisa Hajnal 30-Nov-18 2:43am    
Is there a reason you cannot change NameAndScore with GetGameData (just name change) and adding required functionality?
DENDRINOS K 30-Nov-18 3:46am    
Thank you Sinisa !!!
No there is not ... but I am an apprentice with C# and I do not know exactly what to do...

1 solution

Add another field to NameAndScore to hold the "away" information.
eg:
C#
public class NameAndScore {
    public string Name { get; set; }
    public string Score { get; set; }
    public bool Away { get; set; }              
        }

Now just load the required information in the return statement;
C#
return names.Zip(scores, (name, score, away) => new NameAndScore() { Name = name, Score = score, Away = away }).ToList();
 
Share this answer
 
Comments
Maciej Los 3-Feb-19 16:08pm    
5ed!
Richard Deeming 4-Feb-19 12:24pm    
That's not going to work. The Zip method[^] doesn't have an overload which takes a function that accepts three inputs.

You'd need to call Zip twice:
return names
    .Zip(scores, (name, score) => new { name, score })
    .Zip(aways, (z, away) => new NameAndScore { Name = z.name, Score = z.score, Away = away });

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900