如何为每个数组保存数组中的值?

时间:2022-01-20 01:20:54

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:

我是c#的新手,我正在尝试保存从XML解析器读取的一些数据。数据被正确读取,实际上我将它们模制如下:

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?

但现在我想创建一个包含八个单元格的数组,这些单元格将插入到控制台上显示的每个匹配内容中。谁能告诉我该怎么办?

3 个解决方案

#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
            }));

#2


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();

#3


Create a lambda expression with a new anonymous type at runtime

在运行时使用新的匿名类型创建lambda表达式

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:

基本上,你将创建一个匿名对象,但你必须将它返回到某个东西;现在,没有任何东西被分配给liveMatches的结果,所以没有任何东西出来。我会做的事情如下:

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

Edited based on Sidewinder94's comment

根据Sidewinder94的评论编辑

#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
            }));

#2


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();

#3


Create a lambda expression with a new anonymous type at runtime

在运行时使用新的匿名类型创建lambda表达式

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:

基本上,你将创建一个匿名对象,但你必须将它返回到某个东西;现在,没有任何东西被分配给liveMatches的结果,所以没有任何东西出来。我会做的事情如下:

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

Edited based on Sidewinder94's comment

根据Sidewinder94的评论编辑