Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually I got stuck by my own code for searching " all players for a team". I don't know the way to relieve.
I created to model as Team.cs:
C#
public class Team
   {
       public int TeamID { get; set; }
       [Required]
       [Display(Name = "Team")]
       public string TeamName { get; set; }
       public string Details { get; set; }
   }


And Player.cs

C#
namespace FootBall.Models
{
    public class Player
    {
        public int PlayerID { get; set; }
        [Required]
        [Display(Name = "Name")]
        public string PlayerName { get; set; }
        [Display(Name = "Gersy No")]
        public string PlayerPosition { get; set; }
        [Display(Name = "Details")]
        public string PlayerDetails { get; set; }
        public int TeamID { get; set; }
    }
}


I want to get all Player's list of a team while I am sending the team ID to search which will be matched by the the player's team Id, As I provide team ID with the all information of a player.

I wrote a code to search it through TeamRepository.

Here we wrote in TeamController.cs

SQL
public ActionResult Search(int id)
        {
            ViewData["Search"] = repo.PlayerRepository.Search(id);
            return View(ViewData["Search"]);
        }



and PlayerRepository.cs contains

public Player Search(int id)
{
Player player = context.Players.Where(i => i.TeamID == id).FirstOrDefault();
return player;

}

But "player" didn't get nothing.

Would you please help me to ahead? Thanks......
Posted

1 solution

following systex is correct 


C#
Player player = context.Players.Where(i => i.TeamID == id).FirstOrDefault();


You need to check by debugging
you can use following also

C#
var pl = (from x in context.Players
                           where TeamID == id 
                           select x)FirstOrDefault();



and by the inline query


C#
var data = context.Database.SqlQuery<player>
                                   ("select * 
                                   from Players where TeamID=" + id + "", "");</player>
 
Share this answer
 

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