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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19287350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:38:23  来源:igfitidea点击:

Get value of certain index in list C#

c#listcollections

提问by Luigi Vibal

I want to get value of a certain index on my list how can do that? I have here a method that returns List of Games:

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

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

For example I want to display the image url of the first and second item on the list

例如我想显示列表中第一项和第二项的图像 url

can I do something like this?

我可以做这样的事情吗?

<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 .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>

采纳答案by Aleksei Chepovoi

I'm not sure what exactly do You need, but if You want to get value of certain index in list C#You can just do this using square brackets syntax:

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

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

You can do this because List<T>implements IList<T>interface which defines this indexer:

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

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

Also You can use Linq to Objectsto query your List.

您也可以Linq to Objects用来查询您的列表。

Hope this helps.

希望这可以帮助。

回答by milan m

I think the simplest way to resolve is to replace the following code

我觉得最简单的解决方法就是替换下面的代码

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

with an asp.net Image Control and set its src from code-behind.

使用 asp.net 图像控件并从代码隐藏设置其 src。