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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:44:41+00:00 2026-05-21T17:44:41+00:00

I have a simple database with two tables: Photo and Tag . There is

  • 0

I have a simple database with two tables: Photo and Tag. There is a one-to-many (a photo can have many tags) relationship between the two tables. Here is a diagram:

Tables

Now I have made a Photo class and set it up for LINQ-to-SQL using attributes. The code for that class is below:

[Table]
public class Photo
{

    [Column(IsDbGenerated = true, IsPrimaryKey = true, CanBeNull = false)]
    public int ID { get; set; }

    [Column(CanBeNull = false)]
    public string Filename { get; set; }

    [Column(CanBeNull = false)]
    public string Description { get; set; }

    [Column(CanBeNull = false)]
    public DateTime DateTaken { get; set; }


    public List<string> Tags { get; set; }

    public override string ToString()
    {
        string result = String.Format("File: {0}, Desc: {1}, Date: {2}, Tags: ", 
            Filename, Description, DateTaken);
        if (Tags != null)
            foreach (string tag in Tags)
                result += tag + ", ";
        return result;
    }
}

You will notice that currently I do not have any attributes for the Tags list. I would like to be able to setup the attributes (associations) for the Tags list so that it would be populated with Name field of the Tag table for all entries in the Tag table of a particular PhotoID. It would be preferable if I could do this directly (i.e. without having to setup a Tag class mimicking/relating to the Tag table). Since I’m only interested in one field (the Name in the Tag table) rather than many fields, I would think there is a way to do this.

Is this possible, and if so how would I further decorate the class with attributes and what would be the syntax for a simiple Select query using LINQ-to-SQL?

If it helps, here is the code I am using to simply add a new photo and then grab all of the photos out of the database (obviously the tag information is not pulled out as the code stands now).

DataContext context = new DataContext(connectionString);

// add new photo
Photo newPhoto = new Photo { Filename = "MyImage1.jpg", Description = "Me", DateTaken = DateTime.Now };
context.GetTable<Photo>().InsertOnSubmit(newPhoto);
context.SubmitChanges();

// print out all photos
var photoQuery = from m in context.GetTable<Photo>() select m;
foreach (Photo myPhoto in photoQuery)
    textBox1.Text += Environment.NewLine + myPhoto.ToString();
  • 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-21T17:44:42+00:00Added an answer on May 21, 2026 at 5:44 pm

    First I’d suggest you to use a tool to generate your entity classes (the classes that correspond to the database tables). We’r using sqlmetal and it does the job very well.
    Next, (if you have a Tag entity) than write a function that fetches the tags for some photos:

    void GetTags(IEnumerable<Photo> photos)
    {
    var ids = photos.Select(p=>p.ID).ToList();
    var tagsG = (from tag in context.GetTable<Tag>() where ids.Contains(tag.PhotoID) select new {PhotoID, Name}).GroupBy(tag=>tag.PhotoID);
    foreach(ph in photos){
    ph.Tags = tagsG[ph.ID].Select(tag=>tag.Name).ToList();
    }
    }
    

    Note, the code might not compile I’ve written it in the browser…

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

Sidebar

Related Questions

I have a simple application using ADO.NET EntityFramework using a many-to-many relationship between two
I have two tables within the same database. On one I can use the
So essentially I have two tables, containing URLS and TAGS, with a has-and-belongs-to-many relationship
I have a simple database with two tables. Users and Configurations. A user has
I have two simple tables in my database. A card table that contains Id,
I have very simple form along with two database tables. In this form is
I have a simple data model of two tables, email and recipients, email can
My database has two tables, question and field. Questions may have many fields, and
I have a simple database with 2 main tables with a many to many
I have a has_many relationship between two entities, Feeds and Posts. I also have

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.