I have two database tables that are many-to-many
tblBook idBook Title Writer ect...
tblBookGenre id idBook idGenre
tblGenre idGenre NameGenre
And tblBook idBook Title Writer ect...
tblBookTag id idBook idGenre
tblTag idTag NameTag
I map them like this
public class Book:INotifyPropertyChanged
{
Connection con = new Connection();
private int _idBook;
private string _title;
private List<Genre> _genres;
private List<Tag> _tag;
private ObservableCollection<Book> _books;
public int IdBook
{
get { return _idBook; }
set { _idBook = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public List<Genre> Genres
{
get { return _genres; }
set { _genres = value; }
}
public List<Tag> Tags
{
get { return _tag; }
set { _tag = value; }
}
public ObservableCollection<Book> Books
{
get { return _books; }
set { _books = value; }
}
public Book()
{
Tags = new List<Tag>();
Books = new ObservableCollection<Book>();
Genres = new List<Genre>();
}
I use a sqlcommand and reader to read from the database
public ObservableCollection<Book> GetBooks()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con.con;
cmd.CommandText = "SELECT idBook,Title FROM tblBook WHERE Active = 1 ORDER BY b.idBook";
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
{
Book book = new Book();
var id = Convert.ToInt32(rd["IdBook"]);
var title = rd["Title"].ToString();
book.IdBook = id;
book.Title = title;
List<Tag> tags = GetTagsThatMatch(id);
book.Tags = tags;
book.Genres = GetGenresThatMatch(id);
Books.Add(book);
}
rd.Close();
}
con.Close();
return Books;
}
public ObservableCollection<Tag> GetTagsThatMatch(int id)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con.con;
cmd.CommandText = "SELECT t.IdTag,t.Name FROM tblTag t LEFT OUTER JOIN tblBookTag bt ON bt.idTag = t.idTag WHERE bt.idBook = @ID";
cmd.Parameters.AddWithValue("@ID", id);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
{
Tag tag = new Tag();
tag.IdTag = Convert.ToInt32(rd["IdTag"]);
tag.Name = rd["Name"].ToString();
Tags.Add(tag);
}
}
return Tags;
}
public List<Genre> GetGenresThatMatch(int id)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con.con;
cmd.CommandText = "SELECT g.Name FROM tblGenre g LEFT JOIN tblBookGenre bg ON g.IdGenre = bg.IdGenre LEFT OUTER JOIN tblBook b ON bg.IdBook = b.IdBook WHERE b.IdBook = @Id";
cmd.Parameters.AddWithValue("@ID", id);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
{
Genre genre = new Genre();
genre.Name = rd["Name"].ToString();
Genres.Add(genre);
}
}
return Genres;
}
Problem is it displays all genres and tags for all books
Book 1 - Genre 1, Genre 2, Genre3 - Tag1, Tag2, Tag3
Book 2 - Genre 1, Genre 2, Genre3 - Tag1, Tag2, Tag3
When it should display only the ones that match
Book 1- Genre 1, Genre 2 -Tag1, Tag2
Book 2 - Genre3 -Tag3
I would really love to do this without an ORM and it seems like its simple, but I cannot find a lot of resources on it without an ORM. Also any articles or examples would be appreciated.