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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:27:37+00:00 2026-05-24T12:27:37+00:00

I have a class that has 3 properties: class Three { int ID {

  • 0

I have a class that has 3 properties:

class Three
{
   int ID { get; set ;}
   string Name { get; set; }
   double Value { get; set; }
}

I then have a linq query that returns a Queryable<Three> but only ID and Name are defined within the table.

var three = from t in db.Threes
            select t;

three = { {ID=1, Name="A", Value=0},{ID=2, Name="B", Value=0},...}

So Value is not defined within the db.Threes table so it defaults to 0 as doubles do. I then iterate over each item in var three to set Value based upon another table:

for (int i = 0; i < three.Count(); i++)
{
   three[i].Value = (from v in db.Values
                     where v.ThreeID = i.ID
                     select v.Value).First();
}

return three;

The Queryable<Three> three now contains definitions for .Value:

three = { {ID=1, Name="A", Value=99},{ID=2, Name="B", Value=100},...}

The key thing that I’m trying to do is to still return a Queryable<Three>, but to also set the Value property.

Is there a better way to do this instead of using a for loop?

I’d like to be able to extend this to any class with a Value property that is assigned from another table; suppose I have a class Four:

class Four
{
   int ID {get; set;}
   string Name {get; set;}
   DateTime Date {get; set;}
   double Value {get; set;}
}

The first query would populate with ID, Name and Date and I still want to be able to assign Value. I feel like there is a better way to do this with linq that I just can’t come up with.

Edit: suppose I’m calling db.GetTable<T> and Three and Four are really generic arguments that implement a .Value interface. I can’t explicitly instantiate a generic type because I would have to hard code the properties.

  • 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-24T12:27:38+00:00Added an answer on May 24, 2026 at 12:27 pm

    I think you are looking for join. You are assigning values from another table whose id is the same as the Three table.

    You can build back a Three object like this:

    var three = from t in db.Threes
                join v in db.Values on t.ID equals v.ThreeID
                select new Three{
                                ID = t.ID, 
                                Name = t.Name,
                                Value = v.Value 
                             };
    

    What you were doing before by looping, that is a performance hit. What happens is that you called to the database to loop it, then for each loop, you make another call back to the database. If your tables are linked by those Ids, then using this code makes 1 single database call and no in memory looping.

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

Sidebar

Related Questions

I have a c# class that has 20+ string properties. I set about a
I have a class that has some properties. And I want something that calculates
I have a Car class. It has three properties: id, color and model. In
I have a class that has only one function Print() and two properties age,
I have a complex value object class that has 1) a number or read-only
Let's say I have a class which has three properties as below. public class
I have a class that is inherited from List<T> and also has some properties,
I have a Query class that has a number of properties that are collections
I have a class that has a Generic type G In my class model
I have a class that has a vector of another class objects as a

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.