我有數據庫表Staff
,其列Name
,Email
,Address
和DateOfBirth
。在我的web應用程序中,我有一個單獨的類ClassMyBase
有幾個方法。其中一種方法使用LINQ到實體:在WebForm1
使用LINQ to Entities時的方法返回數據類型
public static List<Staff> ShowAll()
{
using (ModelPersonnelContainer myContainer = new
ModelPersonnelContainer())
{
return myContainer.Staff.ToList();
}
}
...然後在ButtonShowAll
事件處理程序:
protected void ButtonShowAll_Click(object sender, EventArgs e)
{
GridViewAll.DataSource = ClassMyBase.ShowAll();
GridViewAll.DataBind();
}
到目前爲止好,但如果我添加篩選我public static List<Staff> ShowAll()
:
public static xyz ShowAll()
{
using (ModelPersonnelContainer myContainer = new
ModelPersonnelContainer())
{
selectedRrows=from item in myContainer.Staff
select new
{
Name=item.Name,
Email=item.Email
}
}
}
我的方法將無法工作,因爲返回數據類型不再像以前一樣。任何簡單的方法這個返回數據類型xyz
是什麼?
如果我把一切都放在一起(在我的項目沒有單獨的類),並只ButtonShowAll
它會工作沒事,所以像這樣的:
protected void ButtonShowAll_Click(object sender, EventArgs e)
{
using (ModelPersonnelContainer myContainer = new
ModelPersonnelContainer())
{
var selectedRows = from item in myContainer.Staff
select new
{
Name=item.Name,
Email=item.Email
};
GridView1.DataSource = selectedRows.ToList();
GridView1.DataBind();
}
你可以用'名單'因爲你的類型 –
AlexanderBrevig
2014-12-08 09:35:21
你需要添加在靜態方法中過濾後的'.ToList()'返回一個員工列表 – Mivaweb 2014-12-08 09:35:34