[英]How can I make this C# class and method static?
我是一名新的ASP.NET開發人員,我正在嘗試學習Linq-To-Entities。 我正在嘗試在我的數據訪問層中創建一個名為Items的靜態類。 此類有一個方法來檢索Items Entity中的所有記錄。 然后,我將使用它將GridView綁定到它。 問題是當我將該類作為靜態類並且我不知道原因時,我在getData()方法中遇到以下錯誤:
C#代碼:
public static class Items
{
//properties
public int ID { get; set; }
public string Code { get; set; }
public int ItemTypeID { get; set; }
public string Name { get; set; }
public int StatusID { get; set; }
public static IEnumerable<Items> getData()
{
List<Items> itemsList = new List<Items>();
using (ItemsDBEntities context = new ItemsDBEntities())
{
itemsList = (from item in context.Items
select new Items()
{
ID = item.ID,
Code = item.Code,
Name = item.Name,
StatusID = item.StatusID
}).ToList();
}
return itemsList;
}
你能告訴我如何解決這個錯誤嗎?
你能告訴我如何解決這個錯誤嗎?
//public static class Items
public class Items
{
}
Items
不應該是static
有明顯的原因。
getData()
方法可以保持靜態,盡管這似乎不是必需或有用的。 它只是讓測試變得更難。 在ASP.NET中研究狀態管理和靜態的使用。
Id
和Name
屬性的任何內容都是Item
,而不是Items
。 GetData()
。 只需刪除Items類的靜態聲明即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.