两个表与foreigns键连接

时间:2016-09-12 12:48:35

标签: sql-server linq linq-to-sql

让我们直接看看代码。 我试图通过类别的内连接表返回电影列表。到目前为止失败了。

private extractData(res:Response) {
  let body = res.json();
  return body.data || { }; <== notice data instead Data
}

我对linq很新,所以这可能是错的。

public int FilmID { get; private set; }
public string FilmName { get; set; }
public string FilmDescription { get; set; }
public int FilmYear { get; set; }
public string FkCategoryID { get; set; }

当然会失败 public List<Movie> TakeOutAllMovies() { var WholeList = (from movies in db.films join cat in db.categories on movies equals cat.category_id select new Movie{FilmID = movies.film_id, FilmName = movies.film_navn, FilmDescription = movies.film_description, FilmYear = movies.film_year, FkCategoryID = movies.categories.category_navn}).ToList(); return WholeList; }

我想从表类别而不是ID中取代fkCategoryID类别的名称。我做得对吗?

1 个答案:

答案 0 :(得分:1)

您不应将Movie加入category_id,而应加入电影FkCategoryId

所以而不是

from movies in db.films          
join cat in db.categories on movies equals cat.category_id

这样:

from movies in db.films 
join cat in db.categories on movies.FkCategoryID equals cat.category_id