2014-10-27 112 views
0

我使用實體框架在Database.cs中有兩個表(Players,Clubs)。我想要將Web服務中的數據顯示到控制檯應用程序(客戶端)中。如何顯示來自實體框架數據庫的數據?

我想顯示某個俱樂部的所有球員。

它看起來應該是這樣的。

控制檯應用彈出並說:您想從哪個俱樂部展示球員?

我輸入:「洛杉磯湖人隊」。

應用程序現在應該顯示所有來自洛杉磯湖人隊的球員。

這是我的代碼:

[WebMethod] 
public string playerClub(string clubName) 
{ 
    using (var db = new Database()) 
    { 
     string player = ""; 

     for (int i = 0; i < db.playerList.Count; i++) 
     { 
      if (db.playerList[i].Clubs == clubName) 
      { 
       player = player + "\n" + db.playerList[i].Name + db.seznamUporabnikov[i].LastName; 
      } 
     } 

     return player; 
    } 
} 

它給了我一個錯誤:

Error 2 Operator '<' cannot be applied to operands of type 'int' and 'method group'

能否請你幫我改這個代碼,以便它是否行得通呢?

回答

1

在你的if語句中,你指定了玩家的值,但是好像你想每次添加。因此,您可能會將現有字符串添加到現有字符串中,而不是將玩家等值設置爲您所獲得的值

說到方法組,此鏈接將解釋here

從鏈接:

A method group is the name for a set of methods (that might be just one) - i.e. in theory the ToString method may have multiple overloads (plus any extension methods): ToString(), ToString(string format), etc - hence ToString by itself is a "method group".

0

這是最有可能是因爲db.playerList.Count不是財產,而是一種方法。這意味着playerList不是List<T>,而是其他一些類型,一般可以是IEnumerable<T>或。對於可枚舉,Count是一個方法,所以你應該使用以下行:

for (int i = 0; i < db.playerList.Count(); i++) 

但是,這將潛在地列舉不止一次枚舉多,所以最好:

string result = ""; // changed the name of the variable 
        // to better illustrate its purpose 

foreach (var player in db.playerList) 
{ 
    // use player instead of db.playerList[i] 
} 

你可以閱讀更多關於延遲評估(或推遲執行here