stackoverflow tags and related tags
        Posted  
        
            by parminder
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by parminder
        
        
        
        Published on 2010-05-20T19:39:09Z
        Indexed on 
            2010/05/20
            19:40 UTC
        
        
        Read the original article
        Hit count: 331
        
Hi Experts, I am working on a website where a user can add tags to their posted books. It is similar to stackover flow, but I am keeping my tags in differnt table.
so here are the tables/class in linq to entities.
Books
{
bookId,
Title
}
Tags
{
Id
Tag
}
BooksTags
 {
 Id
 BookId
 TagId
 }
Here are few sample records.
Books
BookId Title
113421  A
113422  B
Tags
Id Tag
1  ASP 
2  C#
3  CSS
4  VB
5  VB.NET
6  PHP
7  java
8  pascal
 BooksTags   
 Id  BookId  TagId
 1  113421    1
 2  113421    2
 3  113421    3
 4  113421    4
 5  113422    1
 6  113422    4
 7  113422    8
Question 1 : I need to write something in linq to entities queries which gives me data according to the tags say if I want bookIds where tagid =1 it should return bookid 113421 and 113422 as it exists in both the books, but If I ask data for tags 1 and 2 it should return only book 113421 as that is the only book where both the tags are present.
Question 2 : I need tags and their count too to show in related tags, so in first case my related tags class should have following result. RelatedTags Tag Count 2 1 3 1 4 2 8 1
in the second case when two tags are requested the result should be like RelatedTags Tag Count 3 1 4 1
I have get the first thing working by converting a sql query in linqer, but that seems like a hell. so want to know if there is any better idea. I have used dyanmic where clause to include two tags. So if someone can help. It will be much appreciated.
Thanks
Parminder
© Stack Overflow or respective owner