Adding Index ActionResult (2)

Index action responsibles for getting the data from SQL table, convert to list and throw the list to view.

public ActionResult Index()
{
    IList<SongModel> SongList = new List<SongModel>();
    var query = ......
    SongList = query.ToList();

    return View(SongList);
}

That not looks intimidating, right?

The key idea is how to query items from SQL to be put in "query" variable. The step is join the tables (song and genre) with same ID then crate a new object.

var query = from song in context.Songs
                join genre in context.Genres
                on song.GenreId equals genre.Id
                select new SongModel
                {
                    Id = song.Id,
                    Title = song.Title,
                    Singer = song.Singer,
                    Year = song.Year,
                    Writer = song.Writer,
                    GenreName = genre.Name
                };

results matching ""

    No results matching ""