此代码有什么问题?代码工作,但它返回空gridview? 如果我调试代码,我可以看到我的字符串plName包含播放器名称,但是当我将它添加到对象时它是空的?另外,我如何检查我的查询结果是否为空?我相信这不是最佳方式......
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
List<AllPlayers> listOfPlayers = new List<AllPlayers>();
string plName = "";
string plStatus = "";
using (PlayerEntities playerEntities = new PlayerEntities())
{
var query = (from pl in playerEntities.players from st in playerEntities.player_status.Where(a => a.players_key == pl.players_key).DefaultIfEmpty()
from pt in playerEntities.play_times.Where(b => b.players_key == pl.players_key).DefaultIfEmpty() select new { pl, st, pt }).ToList();
foreach (var item in query)
{
plName = item.pl.player_name.ToString();
try // better way check if this is NULL ?
{
plName = item.st.default_times_start.ToString();
}
catch(Exception ex)
{
ex.Message.ToString();
}
AllPlayers demo = new AllPlayers(plName, plStatus);
listOfPlayers.Add(demo);
}
}
GridView1.DataSource = listOfPlayers;
GridView1.DataBind();
}
}
ASPX:
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>