Reputation: 28545
I've set up a tagging system
I belive it is the "toxi" solution with the following tables:
Books[id,author, title] has many-to-many relationship with
Tags[id, tagName]
BookHasTag[id, bookId, tagId] junction-table
using linq-sql I want to:
Please can you help, I know it's a little cheeky, and I know SO usually like to see you have made an effort, but for the sake of brevity i'm leaving out my failed attempts.
Upvotes: 0
Views: 245
Reputation: 31239
If I understand you correct you want something like this:
select the top 20 most popular tags and have their count with it
var lsTags=(
from tag in db.Tags
select new
{
tag.id,
tag.tagName,
NbrOfTags=
(
from hasTags in db.BookHasTag
where hasTags.tagId==tag.id
select hasTags.id
).Count()
}
).OrderByDescending(a=>a.NbrOfTags).Take(20).ToList();
given a books id, get all the info about the book, including tags associated with it
var bookId=1;
var lsBooksWithTags=(
from book in db.Books
where book.id==bookId
select new
{
book.id,
book.author,
book.title,
Tags=(
from bookHasTag in db.BookHasTag
join tags in db.Tags
on bookHasTag.tagId equals tags.id
where bookHasTag.bookId==book.id
select new
{
tags.id,
tags.tagName
}
)
}
).ToList();
a list of all books that are associated with a particular tag
var tagId=1;
var lsBooksByTagId=(
from book in db.Books
where
(
db.BookHasTag.Where(a=>a.tagId==tagId).Select(a=>a.bookId)
).Contains(book.id)
select new
{
book.id,
book.author,
book.title
}
).ToList();
Upvotes: 1