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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:58:51+00:00 2026-06-17T11:58:51+00:00

I am trying to simulate a foreign key behaviour using JSON.Net but I can’t

  • 0

I am trying to simulate a foreign key behaviour using JSON.Net but I can’t seem to find a way to have the real references.

Let’s say I have this simple example:

private static void Main(string[] args)
        {
            var g1 = new Group {Name = "g1"};
            var g2 = new Group {Name = "g2"};

            var users = new[]
            {
                new User{ Username = "truc", Group = g1 },
                new User{ Username = "machin", Group = g2 },
                new User{ Username = "dom", Group = g2 },
                new User{ Username = "boum", Group = g2 }
            };

            string json = JsonConvert.SerializeObject(users);

            var jsonUsers = JsonConvert.DeserializeObject<User[]>(json);

            Console.WriteLine(jsonUsers[2].Group == jsonUsers[3].Group);

            Console.ReadLine();
        }

the problem here is that Console.WriteLine(jsonUsers[2].Group == jsonUsers[3].Group); is always false.

The only way I found that would make it work would be to store a list of users, then a list of group and having a GroupId proprety Inside of users. Then after everything is deserialized, I manually insert references of groups Inside of users. It’s hackish.

What would be the best way to approach this problem ?

  • 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-17T11:58:53+00:00Added an answer on June 17, 2026 at 11:58 am

    You are making the instance-comparison. you should override Equals and GetHashcode in Group class. Operator overloading would also be good since you use == operator in Console.WriteLine

    Otherwise;

    new Group() { Name = "a" } == new Group() { Name = "a" }
    

    or

    new Group() { Name = "a" }.Equals(new Group() { Name = "a" })
    

    would always return false

    .

    public class Group
    {
        public string Name;
        public int i;
    
    
        public static bool operator ==(Group a, Group b)
        {
            return a.Equals(b);
        }
    
        public static bool operator !=(Group a, Group b)
        {
            return !(a.Name == b.Name);
        }
    
        public override bool Equals(object obj)
        {
            var g = obj as Group;
            if (ReferenceEquals(this,g)) return true;
            return g.Name.Equals(Name);
        }
    
        public override int GetHashCode()
        {
            return Name.GetHashCode();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to simulate some code that I have working with SQL but using
I'm trying to simulate a particle system using OpenGl but I can't get it
I'm trying to simulate a token ring using python with sockets, but I have
I am trying to simulate an intersection using threads and mutex locks. I have
i am trying to simulate the pressing of the TAB key. I have search
I'm trying to simulate a realistic key press event. For that reason I'm using
I am trying to simulate linux command ls using linux api from c. Looking
I'm currently trying to simulate holding a key down for a specified time. The
I'm trying to simulate rotating box using Newton Physics and OpenGL. This is what
I am trying to simulate a simple MIPS processor using behavior code in Verilog.

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.