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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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()orInsertAllOnSubmit(), and then callSubmitChanges()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#).
We don’t strictly need the
idcolumn, 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
partialto 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.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:
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 fromDataContextthat lets does this for you:(Again, use DBML and have this done for you).
Now, lets start by adding your example list of three coordinates:
(becomes a bunch of SQL inserts)
So far, so boring. Let’s add 500 new entries instead:
(becomes a bunch of SQL inserts)
Lets find out how many we have:
(Becomes the SQL
SELECT COUNT(*) from Coordinates);Let’s find out how many we have where X is greater than Y:
(Becomes the SQL
SELECT COUNT(*) FROM Coordinates where x > y)Let’s delete all that aren’t positive for both X and Y
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.