我正在使用 LINQ/C# 并尝试从函数返回查询的结果。我所拥有的在下面并且不起作用。有什么建议么?我知道这应该进入我的模型,但一步一步。当我可以让它工作时,我将转向模型。
目前我在 button1_Click 中的列表框只返回表名。示例表 1.表 2。
public IQueryable runDBQuery()
{
Variables obj = new Variables();
var urlList = from SURL in db.SteamURLs
where obj.UserID == SURL.uID
select SURL;
dataGridView1.DataSource = urlList;
return urlList;
}
private void button1_Click(object sender, EventArgs e)
{
var urlList = runDBQuery();
// This doesn't work?
listBox1.DataSource = urlList;
}
回答:
public IList<SteamURL> runDBQuery()
{
Variables obj = new Variables();
var query = from SURL in db.SteamURLs
where obj.UserID == SURL.uID
select SURL;
var urlList = query.ToList();
dataGridView1.DataSource = urlList;
urlList.ToList();
return urlList;
}
private void button1_Click(object sender, EventArgs e)
{
var urlList = runDBQuery();
// This doesn't work?
listBox1.DataSource = urlList;
listBox1.DisplayMember = "itemURL";
}