Reputation: 969
I have two tables (Players
, Clubs
) in Database.cs using Entity Framework. I want to display data from web service into console app (client).
I would like to display all players from a certain club.
It looks should look like this.
Console app pops up and says: From which club do you want to display players?
I type in: "Los Angeles Lakers".
App should now display all the players from Los Angeles Lakers.
This is my code:
[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;
}
}
It gives me an error:
Error 2 Operator '<' cannot be applied to operands of type 'int' and 'method group'
Can you please help me change this code so it will work?
Upvotes: 0
Views: 953
Reputation: 35901
This is most probably because the db.playerList.Count
is not a property, but a method. Which implies that playerList
is not List<T>
, but some other type, in general it can be IEnumerable<T>
or . For enumerables, Count
is a method, so you should use the following line:
for (int i = 0; i < db.playerList.Count(); i++)
But this will potentially enumerate the enumerable more than once, so better:
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]
}
You can read more about lazy evaluation (or deferred execution) here.
Upvotes: 0
Reputation: 11
Inside your if statement you assign the value of the player, but it seems like you would like to add each time. So instead of setting player equals to what you get, you would probably add to the existing string.
When it comes to method groups, this link will explain here.
From the link:
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".
Upvotes: 1