views:

29

answers:

2

I am using linq to sql , and I return a queryable result from linq to sql :

var qry = from p in table select p;

Then I use this to bind to a xtragrid:

GridControl.DataSource = qry;

Then If I edit the records in xtraGrid, I just need to call dataContext.submitChanges() to submit the changes back to database.

My question is :

Am I possible to just add new records into the qry result, and after that I only need to call dataContext.submitChanges(), then linq can create new records on database automatically ?

Is that possible ? Can someone point me the right direction ? Thanks in advance !

+1  A: 

Shot answer would be no. You need to call the Add method on the table property on your context.

Something like this:

var qry = from p in myDataContext.Table select p;

GridControl.DataSource = qry;

myDataContext.Table.Add(newRecord); // This is how to add new recrod

dataContext.submitChanges();

Read more here: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

Martin Ingvar Kofoed Jensen
That's what I don't want to do ...
MemoryLeak
I know, but im pretty sure thats the only way to do it. Sorry.
Martin Ingvar Kofoed Jensen
Thanks very much !
MemoryLeak
A: 

You are aware that if you use a LinqDataSource instead of giving a query to your Grid's DataSource property, SubmitChanges, Add, Delete & friends will be called automatically for you, right? If you need to display a subset of the table, you can still override the LinqDataSource's Selecting event to feed it your own query.

nikie