tags:

views:

2235

answers:

2

Hi all, In my database, I have a table named Students, with 3 Columns (SNo, SName, Class).

I want to insert the value of only SName.

Can anybody tell me how to write the LINQ Query for this.

Thanks, Bharath.

+3  A: 

Do you mean you want to query only the name? In which case:

var names = ctx.Students.Select(s=>s.Name);

or in query syntax:

var names = from s in ctx.Students
            select s.Name;

To insert you'd need to create a number of Student objects - set the names but not the other properties, and add them to the context (and submit it). LINQ is a query tool (hence the Q); insertions are currently object oriented.

Marc Gravell
+2  A: 

Are you using Linq-to-SQL ? Do you want to insert a new record while only specifying the Name?

If so, this is roughly how it's done in C#.

StudentDataContext db = new StudentDataContext();
Student newStudent = new Student();
newStudent.SName = "Billy-Bob";
db.InsertOnSubmit(newStudent);
db.SubmitChanges();
Damien
Thank you...But, actually I want to insert only SName into the Students table. The above query works fine for this.Here, when we insert only the SName by using the above mentioned Quesry, what values did the table takes to the other two fields..SNo and Class ???Please clarify me on this.Thanks
SNo and Class will contain the default values for the database. They will be the same as if you had manually added a new row in the database.
Damien