如何从列表对象中读取数据,其中包含数组

时间:2017-10-22 01:17:26

标签: c# asp.net

在下面的代码中创建leaguelist后,如何访问列表中的值以填充其他列表?

我需要将列表的值传递给另一个列表并将其发送到视图。

List<Notification> notificationlist = new List<Notification>(); 
notificationlist.Add(new Notification { date = leaguename = leaguevenue = teamwon = })

我要做的是阅读leaguelist中的值,我需要在notificationlist中访问该列表的数据,以填充date,leaguename,associationvenue等的值。

 List<string[]> leaguelist = new List<string[]>();
        while (count != 0)
        {
            Int16 id = Convert.ToInt16(sportds.Tables[0].Rows[count-1]["sports_details_id"].ToString());

            using (SqlDataAdapter leagueDetails = new SqlDataAdapter("select league_name, league_details_venue,league_details_date from leagues, league_details where sports_details_id1 in ('" + id+ "') or sports_details_id2 in ('" + id + "')", sqlConnection1))
            {
                DataSet leagues = new DataSet();
                leagueDetails.Fill(leagues).ToString();

                lname = leagues.Tables[0].Rows[0]["league_name"].ToString();
                lvenue = leagues.Tables[0].Rows[0]["league_details_venue"].ToString();
                ldate = leagues.Tables[0].Rows[0]["league_details_date"].ToString();

                string[] fields = new string[3];
                fields[0] = lname;
                fields[1] = lvenue;
                fields[2] = ldate;
                leaguelist.Add(fields);                   
            }
            count = count - 1}

请帮帮我!!

2 个答案:

答案 0 :(得分:0)

根据您的评论,您可以浏览列表并阅读foreach循环中的值并将其添加到notificationList

List<Notification> notificationlist = new List<Notification>();

foreach (string[] fields in leagueList)
{
    var lname = fields[0];
    var lvenue = fields[1];
    var ldate = fields[2];

    Console.WriteLine($"League {lname} at {lvenue} on {ldate}");
    notificationsList.Add(new Notification
                              {
                               date = ldate, 
                               leaguename = lname, 
                               leaguevenue = lvenue
                              };
}

您也可以使用LINQ,如下所示:

notificationsList.AddRange(
            leagueList.Select(fields => new Notificationnew Notification
                              {
                               date = fields[2], 
                               leaguename = fields[0], 
                               leaguevenue = fields[1]
                              });

答案 1 :(得分:0)

这可能会有所帮助,您可以从列表中选择一个元素并将其添加到通知列表中。您可以使用for循环遍历列表并执行类似的操作。

var s = leaguelist[0];
        notificationList.Add(new Notification
        {
            date = s[0],
            leagename = s[1],
            leaguevenue = s[2]
        });