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

  • Home
  • SEARCH
  • 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 8437701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:30:25+00:00 2026-06-10T07:30:25+00:00

What if I have a table structured as follows? X – Y 3 /

  • 0

What if I have a table structured as follows?

X – Y

3 / 10

5 / 7

2 / 15

So those are some kind of coordinates. How would I write them to a SQL-server table via LINQ to SQL?

In other words how do I save coordinate points of a graph to a single table?

  • 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-10T07:30:27+00:00Added an answer on June 10, 2026 at 7:30 am

    Yes. Normal database tables are “2-d” as this is, there’s a set of columns with names and types along one dimension, and zero or more rows along another.

    Note that this has nothing to do with “X” and “Y” representing points in a space in a 2-d space. If you had “X”, “Y” and “Z”, representing points in a 3-d space, then the data would still be 2-d.

    As such, it’s the same as any other database table, so just use Linq2Sql, EntityFramework, Linq2NHibernate, or whatever other database query source for linq you want, and write to the table in the means it has. E.g. with Linq2SQL you can change fields on an entity obtained from the table or add new records with InsertOnSubmit() or InsertAllOnSubmit(), and then call SubmitChanges() on the datacontext.

    Let’s take a Linq2SQL example, though much the same is true of the other approaches.

    Let’s have a table in SQL first (alternatively, you can go the other way around, personally I prefer to do each separately by hand, but there are tools for both creating the C# from the SQL and the SQL from the C#).

    CREATE TABLE Coordinates
    (
      id int identity primary key not null,
      x int not null,
      y int not null
    )
    

    We don’t strictly need the id column, but it’ll make life easier for us in a lot of ways, especially if we want to have more than one instance of the same x and y – there has to be some sort of id to make updating possible.

    Now, the next bit will seem more complicated than it really is. In reality, if this was produced from DBML produced from examining the DB, then it’ll be using partial to put most of the implementation in files you don’t need to look at. Otherwise if you were doing it by hand, you can use inheritance from a common class to use re-use on much of the repetiton to this.

    [Table(Name="dbo.Coordinates")]
    public class Coordinate : INotifyPropertyChanging, INotifyPropertyChanged
    {
        public event PropertyChangingEventHandler PropertyChanging;
        public event PropertyChangedEventHandler PropertyChanged;
        private int _id;
        private int _x;
        private int _y;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }
        protected void OnPropertyChanging(string name)
        {
            PropertyChangingEventHandler handler = PropertyChanging;
            if (handler != null)
                handler(this, new PropertyChangingEventArgs(name));
        }
    
        [Column(Storage = "_id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                if(_id != value)
                {
                    SendPropertyChanging("Id");
                    _id = value;
                    SendPropertyChanged("Id");
                }
            }
        }
        [Column(Storage = "_x", DbType = "Int NOT NULL")]
        public int X
        {
            get
            {
                return _x;
            }
            set
            {
                if(_x != value)
                {
                    SendPropertyChanging("X");
                    _x = value;
                    SendPropertyChanged("X");
                }
            }
        }
        [Column(Storage = "_y", DbType = "Int NOT NULL")]
        public int Y
        {
            get
            {
                return _y;
            }
            set
            {
                if(_y != value)
                {
                    SendPropertyChanging("Y");
                    _y = value;
                    SendPropertyChanged("Y");
                }
            }
        }
    }
    

    As said, this is making things look a bit worse that what you’ll actually be dealing with. You can mostly think of the class as being like:

    public class Coordinate
    {
        public int Id { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
    }
    

    Still, despite this being all you have to worry about, some people who favour the other approaches make the amount of boilerplate one of their arguments.

    Anyway, you also have a datacontext class that lets deal with all of this. If you call db.GetTable<Coordinate>() it returns an object that represents the table in the database. Because you will do this a lot, it’s common to create a property on a class derived from DataContext that lets does this for you:

    public Table<Coordinate> Coordinates
    {
      get { return GetTable<Coordinate>(); }
    }
    

    (Again, use DBML and have this done for you).

    Now, lets start by adding your example list of three coordinates:

    using(var db = new OurDataContext(connString))
    {
      var tab = db.Coordinates;
      tab.InsertOnSubmit(new Coordinates{X=3,Y=10});
      tab.InsertOnSubmit(new Coordinates{X=5,Y=7});
      tab.InsertOnSubmit(new Coordinates{X=2,Y=15});
      db.SubmitChanges();
    }
    

    (becomes a bunch of SQL inserts)

    So far, so boring. Let’s add 500 new entries instead:

    private static IEnumerable<Coordinate> ProduceRandomCoordinates(int num)
    {
      var rnd = new Random();
      while(num-- > 0)
        yield return new Coordinate{X=rnd.Next(-100, 101), Y=rnd.Next(-100, 101)};
    }
    
    using(var db = new OurDataContext(connString))
    {
      var tab = db.Coordinates;
      tab.InsertAllOnSubmit(ProduceRandomCoordinates(500));
      db.SubmitChanges();
    }
    

    (becomes a bunch of SQL inserts)

    Lets find out how many we have:

    using(var db = new OurDataContext(connString))
      Console.WriteLine(db.Coordinates.Count());
    

    (Becomes the SQL SELECT COUNT(*) from Coordinates);

    Let’s find out how many we have where X is greater than Y:

    using(var db = new OurDataContext(connString))
      Console.WriteLine(db.Coordinates.Count(co => co.X > co.Y)
    

    (Becomes the SQL SELECT COUNT(*) FROM Coordinates where x > y)

    Let’s delete all that aren’t positive for both X and Y

    using(var db = new OurDataContext(connString))
    {
      var tab = db.Coordinates;
      tab.DeleteAllOnSubmit(tab.Where(co => co.X < 0 || co.Y < 0));
      db.SubmitChanges();
    }
    

    And so on, and so forth. The really useful queries are those where we are joining multiple tables and selecting subsets based on criteria, and then adapting them into anonymous objects containing the information we care about on the state of the entire database.

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

Sidebar

Related Questions

Problem Assumption: I have a table in SQL Server, with the structure as follows;
I'm making a database and have a table structured as follows Att1 Att2 Att3
I have a table structured as follows: table(A, B) They are both primary keys
I have an sqlite database structured as follows: CREATE TABLE IF NOT EXISTS Patient
I have a table and the structure of it is as follows: VoteId QuestionId
I have a table structured as following: I'm trying to select the latest handicap
I have a table structured as: create table a ( a bigint primary key,
I have a table structured like this: <table> <tr id=id1 class=parent></tr> <tr class=id1></tr> <tr
I have a MySQL table structured somewhat like this: type name value ===================== 1
I have a posts table structured this way: id | title | content |

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.