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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:25:13+00:00 2026-06-18T01:25:13+00:00

In my database I have 2 tables: id | name | city | street

  • 0

In my database I have 2 tables:

id   | name |  city  | street
-------------------------------
   1 | John | London | Street1
   2 | Will | London | Street1
   3 | Adam | LA     | Street1

id   | uid | phone
------------------
   1 |    1| 12345
   2 |    1| 23456
   3 |    2| 16505
   4 |    3| 65909
   5 |    3| 68902
   6 |    3| 15905

I need to select that data and store into List (or Dictionary as user Id is unique)

My uses class looks like so:

public class User
{
    //lock
    private static readonly object userLock = new object();

    public int      Id        { get; set; }
    public string   Name      { get; set; }
    public string   City      { get; set; }
    public string   Street    { get; set; }

    public List<string> Phones { get; private set; }

    public User()
    {
        Phones = new List<string>();
    }

    public void AddPhone(string phone)
    {
        lock (userLock)
        {
            Phones.Add(phone);
        }
    }
}

Idea is to load those users info into application, add/remove/modify phones and save those changed/removed/added.

I found idea for solution here: https://stackoverflow.com/a/11687743/965722, but I imagine this as doing one query to get users list and then for each user I need separate query to get his phones.

How should I do my loading?
Can I do it with only one query?
How should I populate my result collection?
Should I use List or Dictionary? Then I could remove Id from User class and have it as key inside dictionary.

I’m using .NET 3.5 and would like to avoid Entity Framework or ORM’s.

  • 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-18T01:25:14+00:00Added an answer on June 18, 2026 at 1:25 am

    Make a class Phone and change the User property accordingly:

    public class Phone
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public string Number { get; set; }
    }
    
    public class User
    {
        // (etc)
        public List<Phone> Phones { get; set; }
        // (etc)        
    }
    

    Make a PhoneRepository:

    public class PhoneRepository
    {
        public List<Phone> LoadPhonesFromId(IEnumerable<int> ids)
        {
           // write a query with the ids
           // SELECT * FROM Phones WHERE id IN (@id1, @id2, ...)
           // execute the query
           // convert the results to Phones and put them in a List<Phone>
           // return the list
        }
        public List<Phone> LoadPhonesFromUserId(IEnumerable<int> userIds)
        {
           // write a query with the userIds
           // SELECT * FROM Phones WHERE userId IN (@userId1, @userId2, ...)
           // execute the query
           // convert the results to Phones and put them in a List<Phone>
           // return the list
        }
    }
    

    And a UserRepository:

    public class UserRepository
    {
        public List<User> LoadUsersFromUserIds(IEnumerable<int> userIds)
        {
           // write a query with the userIds
           // SELECT * FROM User WHERE id IN (@id1, @id2, ...)
           // execute the query
           // convert the results to Users and put them in a List<User>
           // return the list
        }
    
        public void IncludePhones(IEnumerable<User> users)
        {            
            var phones = PhoneRepository.LoadPhonesFromUserId
                (users.Select(x => x.Id));
    
            for each(var user in users)
            {
                user.Phones = phones
                    .Where(x => x.UserId == user.Id)
                    .ToList();
            }
        }
    }
    

    You can greatly expand and improve on this pattern with, for example, custom filter parameters (instead of different functions for the various filters) and property setters that make sure the user and user.Phones keep referring to the same userID, but that is sort of beyond the scope of your question.

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

Sidebar

Related Questions

I have two database tables, Team ( ID, NAME, CITY, BOSS, TOTALPLAYER ) and
I have two tables in a MySql database: COUNTRY --------------- id, country_name and CITY
I have 5 tables in my Mysql database with t1 with field name like
I have a database with many tables and one particular table has columns: name
I have a database with few tables, ex.: Employee (Id, Name, state, country) Material
I have the following two tables in my mysql database. Table Name: groups id
I have the following (simplified) database tables: Products - ProductID, int, PK - Name,
I have a MySQL database with two tables in it: Members - id, name,
actually i have 2 tables table1 and table2 table1 name city addr. table2 name
I have the following database tables: table: person Columns: id, first_name, age, city, state

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.