-1

I am new in c # and I'm trying to save some data read from an XML parser. The data is read correctly, in fact I mold them as follows:

liveMatches.ForEach(match => Console.WriteLine(
            match.Id.ToString(),
            match.Name,
            match.Country,
            match.Historical_Data,
            match.Fixtures,
            match.Livescore,
            match.NumberOfMatches,
            match.LatestMatchResult
            ));

but now I want to create an array having eight cells, these cells will be inserted in its content that is shown on the console for each match. Who can tell me how to do?

Harold Finch
  • 576
  • 11
  • 32

3 Answers3

1

Create a class...

public class Match
{
   public string Id {get; set;}
   public string Name {get; set;}
   ....
}

Then, create a list of Match...

var matches = liveMatches.Select(match => new Match {
            Id = match.Id.ToString(),
            Name = match.Name,
            etc
            }).ToList();

I'm hand-typing here, so I'm not sure if this is correct, but it is along these lines.

EDIT: If you can't create a type, you can use...

var matches = liveMatches.Select(match => new 
            {
                Id = match.Id.ToString(),
                Name = match.Name
            }).ToList();
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
1

Try something like this

           List<List<object>> results = new List<List<object>>();
            liveMatches.ForEach(match => results.Add( new object[] {
               match.Id.ToString(),
               match.Name,
               match.Country,
               match.Historical_Data,
               match.Fixtures,
               match.Livescore,
               match.NumberOfMatches,
               match.LatestMatchResult
            }));
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

Create a lambda expression with a new anonymous type at runtime

This should answer your question.

Basically, you'll create an anonymous object, but you'll have to return it to something; right now, nothing is being assigned to the result from liveMatches, so nothing is coming out of it. I would do something like:

var Item = liveMatches
                  .Where(match => match != null)
                  .Select(match => new {
                  Id = match.Id.ToString(),
                  Name = match.Name
                  ...
                  )).ToList();

Edited based on Sidewinder94's comment

Community
  • 1
  • 1
Tyler H
  • 413
  • 3
  • 11
  • If you got the MSDN documentation of your ForEach method, i'd be really interested, because the one i'm using and i could find on MSDN don't return anything : https://msdn.microsoft.com/en-us/library/bwabdf9z%28v=vs.110%29.aspx – Irwene May 13 '15 at 15:29
  • You're right, I mixed up my lambda calls. It should be liveMatches.Where(match => match != null).Select(match => new { etc. }) – Tyler H May 13 '15 at 15:33