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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:46:18+00:00 2026-06-02T00:46:18+00:00

I have the following simple textbook classes defined: public class Product { public int

  • 0

I have the following simple “textbook” classes defined:

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Category { get; set; }
    }

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

The Category field in Product is an integer referencing the Id field in the Category class. In the database, it would be a foreign key relationship between the Product and Category tables.

But my application would need to show the friendly name of the Category of a product.

Q1: Is it correct practice to define a new class as follows:

    public class ProductJ : Product
    {
        public string CategoryName { get; set; }
    }

And then, if I now have a method getProductsJ as follows:

    public class Test
    {
        public List<Category> Categories = new List<Category>() { ... };
        public List<Product> Products = new List<Product>() { ... };

        public List<ProductJ> getProductsJ()
        {
            var products = from p in Products
               join c in Categories on p.Category equals c.Id
               select new ProductJ { Id = p.Id, , Name = p.Name, CategoryName = c.Name }; //!
            return products.ToList();
        }
    }

Q2: Is the above the best way to retrieve a list of Products with Category names?

Q3: In the select statement (//!) is there a faster way to populate the fields of the base class Product linqwithout having to enter them one by one?

Thanks.

  • 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-06-02T00:46:19+00:00Added an answer on June 2, 2026 at 12:46 am

    You could project the result of the join into an object that contains a reference to both the product and category.

    public class Product
    {
      public int Id { get; set; }
      public string Name { get; set; }
      public int Category { get; set; }
    }
    
    public class Category
    {
      public int Id { get; set; }
      public string Name { get; set; }
    }
    
    public class ProductCategory
    {
      public Product Product { get; set; }
      public Category Category { get; set; }
    }
    
    class Program
    {
      static void Main(string[] args)
      {
        List<Category> Categories = new List<Category>();
        List<Product> Products = new List<Product>();
    
        var products = from p in Products
                       join c in Categories on p.Category equals c.Id
                       select new ProductCategory { Product = p, Category = c };
    
        var list = products.ToList();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following simple program: import std.stdio; int main(string[] argv) { writeln(Hello, world!); return
I have the following simple Java code: package testj; import java.util.*; public class Query<T>
I have following simple method in my main activity. private void showMap(String name )
I have the following (simple) mapping: @Entity public class Role { @OneToMany( fetch =
I have the following simple constructor: public SimpleClass(Type1 arg1, int interval = 1000) {
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have the following simple code : int speed1 = (int)(6.2f * 10); float
I have the following simple SQL statment SELECT id, name, value_name, value_id FROM table
In the Photo model I have following simple rule: searchable do string :note end
I have the following simple test which doesnt return true for some reason. string[]

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.