C# 获取列表C中某个索引的值#
我想得到列表中某个索引的值,怎么能做到呢?我这里有一个返回游戏列表的方法:C# 获取列表C中某个索引的值#,c#,list,collections,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
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;
}
publicstaticlist GetGames()
{
List listGame=新列表();
游戏;
SqlConnection con=新的SqlConnection();
尝试
{
con.ConnectionString=连接字符串;
con.Open();
SqlCommand com=newsqlcommand();
com.Connection=con;
com.CommandType=CommandType.StoredProcess;
com.CommandText=“GetDisplayPicOfContest”;
SqlDataReader dr=com.ExecuteReader();
while(dr.Read())
{
game=新的BO.game();
game.PrizeID=Convert.ToInt32(dr[“PrizeID”]);
game.Name=dr[“LocationName”].ToString();
game.ImageURL=“DisplayImages/”+dr[“DisplayImage”].ToString();
game.Country=dr[“CountryName”].ToString();
添加(游戏);
}
Close博士();
dr.Dispose();
}
捕获(例外情况除外)
{
掷骰子;
}
最后
{
con.Close();
con.Dispose();
}
返回列表游戏;
}
例如,我想显示列表中第一个和第二个项目的图像url
我可以这样做吗
<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> 1 </a></li>
<li><a> 2 </a></li>
<li><a> 3 </a></li>
<li><a> 4 </a></li>
<li><a> 5 </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>
选择门票数量门票$5.00
-
Waiohai海滩俱乐部万豪酒店
夏威夷
我认为最简单的解决方法是替换以下代码
<img class="hover-img-main" src="<%=listGames[0].ImageURL;%>" />
“/>
使用asp.net图像控件并从代码隐藏中设置其src。我不确定您到底需要什么,但如果您想获取列表C中某些索引的值35;,您可以使用
方括号语法执行此操作:
List<something> list = GetListOfSomething();
var someItem = list[yourIndex];
您还可以使用Linq to Objects
查询列表
希望这能有所帮助。您是否遵循MVC?您是否尝试过您的代码?@Arun-这是webforms@HamletHakobyan-是的,我已经试过了,但它不是工作列表-将其更改为ToArray()或者,由于设计问题,IEnumarableI无法将其替换为图像控件。您面临什么样的错误。?将代码替换为以下行,然后查看alt文本中出现什么样的url用于测试。错误如下:说明:编译过程中发生错误为该请求提供服务所需的资源的。请查看以下特定错误详细信息,并适当修改源代码。编译器错误消息:CS1026:)应为
object this[int index] { get; set; }