获取列表C#中某些索引的值

时间:2013-10-10 04:54:02

标签: c# list collections

我想在我的列表中获得某个索引的值怎么做?我这里有一个返回游戏列表的方法:

public static List<BO.Game> GetGames()
    {
        List<BO.Game> listGame = new List<BO.Game>();

        BO.Game game;



        SqlConnection con = new SqlConnection();

        try
        {
            con.ConnectionString = connectionString;
            con.Open();

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandType = CommandType.StoredProcedure;
            com.CommandText = "GetDisplayPicOfContest";

            SqlDataReader dr = com.ExecuteReader();

            while (dr.Read())
            {
                game = new BO.Game();
                game.PrizeID = Convert.ToInt32(dr["PrizeID"]);
                game.Name = dr["LocationName"].ToString();
                game.ImageURL = "DisplayImages/" + dr["DisplayImage"].ToString();
                game.Country = dr["CountryName"].ToString();
                listGame.Add(game);
            }


            dr.Close();
            dr.Dispose();


        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally

        {
            con.Close();
            con.Dispose();
        }

        return listGame;
    }

例如,我想在列表中显示第一个和第二个项目的图像网址

我可以做这样的事吗?

<div id="mcont2-sub1" class="mcont-subs mcont-subs-2">
                    <div class="full-detais-cover2">

                        <a class="opac-link" href="game_details.html" ><img  class="hover-img-main" src="<%=listGames[0].ImageURL;%>" /></a>

                        <a class="opac-link" href="game_details.html" ><div id="cover-pager2"><img src="Images/lens-full-details.png" /></div></a>
                        <div class="ticket-quantity2">
                        <span>Choose ticket quantity</span><span>Tickets $5.00</span>
                            <ul class="quantity-paging">
                                <li><a>&nbsp;1&nbsp;</a></li>
                                <li><a>&nbsp;2&nbsp;</a></li>
                                <li><a>&nbsp;3&nbsp;</a></li>
                                <li><a>&nbsp;4&nbsp;</a></li>
                                <li><a>&nbsp;5&nbsp;</a></li>
                                <li><a>10</a></li>
                                <li><a>15</a></li>
                                <li><a>20</a></li>
                            </ul>
                        </div>
                    </div>
                    <div class="descrip-place" id="id-descrip-place">
                        <p class="description">Marbella Beach Club Marriott</p>
                        <p class="place">Spain</p>
                    </div>
                    <a  href="game_details.html" class="arrow-light-blue" ><img src="Images/arrow-light-blue.png" /></a>
                </div>

                <div id="mcont2-sub2" class="mcont-subs mcont-subs-2">
                    <a href="game_details.html" ><img src="<%=listGames[1].ImageURL;%>" /></a>
                    <div class="descrip-place" id="Div1">
                        <p class="description">Waiohai Beach Club Marriott</p>
                        <p class="place">Hawaii</p>
                    </div>
                    <a  href="game_details.html" class="arrow-light-blue" ><img src="Images/arrow-light-blue.png" /></a>
                </div>

2 个答案:

答案 0 :(得分:6)

我不确定您到底需要什么,但如果您想获取列表C#中某些索引的值您可以使用square brackets syntax执行此操作:

List<something> list = GetListOfSomething();
var someItem = list[yourIndex]; 

您可以这样做,因为List<T>实现了定义此索引器的IList<T>接口:

object this[int index] { get; set; }

此外,您可以使用Linq to Objects查询您的列表。

希望这有帮助。

答案 1 :(得分:0)

我认为最简单的解决方法是替换以下代码

<img  class="hover-img-main" src="<%=listGames[0].ImageURL;%>" />

使用asp.net Image Control并从代码隐藏设置其src。

相关问题