Click here to Skip to main content
16,009,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a player controller with property id, Name, age etc.
I want to search a player by name . I wrote the query as follows:

C#
public ActionResult Search(Player player)
{
     Player name = context.Players.Single(x => x.PlayerName == player.ToString()) ; //error is here
        
     return View(name);
}

Would you please tell me what would be the query exactly.

Thanks
Posted
Updated 14-Jun-14 20:03pm
v2

1 solution

1.You forgot to give details about the error;

2.If you want to search by name as you said, you should change your code like this:
C#
public ActionResult Search(Player player)
{
string playerName = palyer.PlayerName; //Should be the player name that come from UI!
Player player= context.Players.FirstOrDefault(x => x.PlayerName == playerName ); 
//Note that the "player" result could be null, so you should manage also this case in your View or here!
return View(player);
}
 
Share this answer
 
Comments
rayhan12345 15-Jun-14 2:45am    
I think something wrong with that. I am now hesitate with Player, player, and PlayerName, Actually what is their task. Would u pls explain in details?? I am beginner.
rayhan12345 15-Jun-14 5:35am    
Yes, I got it!! thanks a lot
Raul Iloc 16-Jun-14 4:02am    
Welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900