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 6189885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:29:24+00:00 2026-05-24T02:29:24+00:00

I have a query where I need to get post + every tag that

  • 0

I have a query where I need to get post + every tag that it has. But since Tags are in separate table it makes things hard for me for two reasons. I am not really sure that I even need to pass two values, maybe I can do this somehow differently.

Note that Model is created automatically using LINQ to SQL Classes.


DB

enter image description here

Code

public ActionResult Edit(int id)
{
    // Get post from DB
    var post = (from p in _db.Posts
                where p.ID == id
                select p).Single();

    // Get PostTags from DB
    var postTag = (from pt in _db.PostTags
                   where pt.PostID == id
                   select pt);

    // Now looping through PostTags in order to find all tag names associated with the post
    string tagList = string.Empty;
    foreach (var pt in postTag)
    {
        var tag = (from t in _db.Tags
                   where t.ID == pt.TagID
                   select t).Single();
        tagList += tag.Name + " ";
    }
    return Content(tagList); // This works as intended, outputs all tags for current post

    return View(); // How can I return post itself + tagList ?
}

Problem

  1. As you can see I want to pass post to my view and it makes sense, but I need to somehow pass the tagList string that contains tags as well. How can I do that?

  2. I think my query is bad. I’m not experienced with SQL/LINQ so this is the only thing that I came up with. Maybe I can do the same thing somewhat "smarter"?

Okey, thank you for answers. Both ViewBag and creating BlogViewModel are good. I will go with BlogViewModel I think. But my second question still remains.

  • 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-24T02:29:25+00:00Added an answer on May 24, 2026 at 2:29 am

    Have you considered creating a ViewModel that would hold both of these objects, and passing that ViewModel to your View? You can also use LINQ to improve your queries.

    (Note : I’m posting this from my phone so the syntax could be off)

    Model:

    public class TaggingModel
    {
       public string TagList {get; set;}
       public PostClass Post {get; set;}
    }
    
    public TaggingModel(string tagList, Post post)
    {
        this.TagList = tagList;
        this.YourPost = post;
    }
    

    Code (using LINQ):

    var post = _db.Posts.Where(pt => pt.PostID == id).Single();
    var postTags = post.PostTags.Where(pt => pt.PostID == id);
    
    string tagList = string.Empty;
    
    foreach(var pt in postTags)
    {
         var tag = _db.Tags.Where(t => t.ID == pt.TagID).Single(); 
         tagList += tag.Name + " ";
    }
    
    TaggingModel model = new TaggingModel(tagList, post);
    
    return View(model);
    

    You could also use other methods to send the message across, such as storing them in the ViewData if you didn’t want to implement a Model for it.

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

Sidebar

Related Questions

I have a partitioned table in SQL Enterprise 2005. I need to query the
i have a input tag which is non editable, but some times i need
I have the following Query and i need the query to fetch data from
I have a query filter written in human readable language. I need to parse
I have a SQL query (MS Access) and I need to add two columns,
I need to execute MDX query from command line (MS AS 2005). I have
I have an access database, with a query made. I need to automate it
I've noticed that a few Wordpress blogs have query statistics present in their footer
I have a query that is dynamically built after looking up a field list
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,

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.