Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 796381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:37:51+00:00 2026-05-14T22:37:51+00:00

I am working on a website where a user can add tags to their

  • 0

I am working on a website where a user can add tags to their posted books, much like is currently done for questions on Stack Overflow.

Classes:

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

Questions

  1. I need to write something in LINQ to entity queries which gives me data according to the tags:

    Query: bookIds where tagid = 1
    Returns: bookid: 113421, 113422

    Query 2: tags 1 and 2
    Returns: 113421

  2. I need tags and their count to 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

Second Case:

RelatedTags
Tag Count
3   1
4   1

How do I do this in LINQ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:37:52+00:00Added an answer on May 14, 2026 at 10:37 pm

    On the first part, the interesting restriction is that the book has to match every tag entered, so a where clause of “where tagid == someId” wouldn’t really work. I envision something like this (LINQ-to-objects example)

    List<int> selectedTagIds = new List<int>() { 1, 2 };
    var query = from book in books
                join booktag in booktags 
                on book.Id equals booktag.BookId 
                join selectedId in selectedTagIds 
                on booktag.TagId equals selectedId 
                group book by book into bookgroup 
                where bookgroup.Count() == selectedTagIds.Count
                select bookgroup.Key;
    

    Which basically performs a join from books to booktags and also to the list of selected tag ids and restricts the selection to where the count of book->tag matches equals the count of selected tag ids.

    To pull the related tags, maybe something like this

    var relatedTags = from book in query // use original query as base
                        join booktag in booktags
                        on book.Id equals booktag.BookId
                        join tag in tags
                        on booktag.TagId equals tag.Id
                        where !selectedTagIds.Contains(tag.Id) // exclude selected tags from related tags
                        group tag by tag into taggroup
                        select new
                        {
                            Tag = taggroup.Key,
                            Count = taggroup.Count()
                        };
    

    Full code for the quick example. Not fully OOP, but you get the idea.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace StackOverflow
    {
        class Program
        {
            static void Main()
            {
                List<Book> books = new List<Book>() 
                {
                    new Book() { Id = 113421, Title = "A" },
                    new Book() { Id = 113422, Title = "B" }
                };
    
                List<Tag> tags = new List<Tag>()
                {
                    new Tag() { Id = 1, Name = "ASP" },
                    new Tag() { Id = 2, Name = "C#" },
                    new Tag() { Id = 3, Name = "CSS" },
                    new Tag() { Id = 4, Name = "VB" },
                    new Tag() { Id = 5, Name = "VB.NET" },
                    new Tag() { Id = 6, Name = "PHP" },
                    new Tag() { Id = 7, Name = "Java" },
                    new Tag() { Id = 8, Name = "Pascal" }
                };
    
                List<BookTag> booktags = new List<BookTag>()
                {
                    new BookTag() { Id = 1, BookId = 113421, TagId = 1 },
                    new BookTag() { Id = 2, BookId = 113421, TagId = 2 },
                    new BookTag() { Id = 3, BookId = 113421, TagId = 3 },
                    new BookTag() { Id = 4, BookId = 113421, TagId = 4 },
                    new BookTag() { Id = 5, BookId = 113422, TagId = 1 },
                    new BookTag() { Id = 6, BookId = 113422, TagId = 4 },
                    new BookTag() { Id = 7, BookId = 113422, TagId = 8 }
                };
    
    
                List<int> selectedTagIds = new List<int>() { 1,2 };
    
                // get applicable books based on selected tags
    
                var query = from book in books
                            join booktag in booktags
                            on book.Id equals booktag.BookId
                            join selectedId in selectedTagIds
                            on booktag.TagId equals selectedId
                            group book by book into bookgroup
                            where bookgroup.Count() == selectedTagIds.Count
                            select bookgroup.Key;
    
                foreach (Book book in query)
                {
                    Console.WriteLine("{0}\t{1}",
                        book.Id,
                        book.Title);
                }
    
                // get related tags for selected tags
    
                var relatedTags = from book in query // use original query as base
                                  join booktag in booktags
                                  on book.Id equals booktag.BookId
                                  join tag in tags
                                  on booktag.TagId equals tag.Id
                                  where !selectedTagIds.Contains(tag.Id) // exclude selected tags from related tags
                                  group tag by tag into taggroup
                                  select new
                                  {
                                      Tag = taggroup.Key,
                                      Count = taggroup.Count()
                                  };
    
                foreach (var relatedTag in relatedTags)
                {
                    Console.WriteLine("{0}\t{1}\t{2}",
                        relatedTag.Tag.Id,
                        relatedTag.Tag.Name,
                        relatedTag.Count);
                }
    
                Console.Read();
            }
        }
    
        class Book
        {
            public int Id { get; set; }
            public string Title { get; set; }
        }
    
        class Tag
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    
        class BookTag
        {
            public int Id { get; set; }
            public int BookId { get; set; }
            public int TagId { get; set; }
        } 
    }
    

    So for selected tags 1 & 2, you’ll get book A, and the related tags would be 3 (CSS) and 4 (VB).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a website where a user can implement a C# code solution
I am working on a website, where user can upload photos of product they
i'm working on a website where users can choose various events and add them
I am working on is a website which features: user signup user login add
I'm working on a website that requires us to log a user out after
I'm in the process of working on a user profile system for a website
I'm trying to add some keyboard support for a website I'm working on and
I'm trying to add a payment method to working website that is using a
Background: I'm working on an e-commerce website. It was my original intention to add
I am currently working on an ASP.NET MVC3 website. I have a simple authentication

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.