LINQ in Asp.Net
Simple SELECT,
DELETE Using LINQ to SQL
Language integrated query (LINQ) is a Microsoft .NET framework programming model, which adds query capabilities to the .NET programming languages. These extensions provide shorter and expressive syntax to manipulate data. A number of features have been added to C# and Visual Basic to support LINQ. It envelops powerful querying on objects, XML files, and databases.
LINQ encapsulates heavy generics. Distinguishing features include extension methods, lambda expressions, an object initializer, query syntax, and anonymous types. These are language extensions to enhance syntactic performance to queries.
LINQ encapsulates heavy generics. Distinguishing features include extension methods, lambda expressions, an object initializer, query syntax, and anonymous types. These are language extensions to enhance syntactic performance to queries.
LINQ Operations:
Step 1: Create a COURSE Table in the database
CREATE TABLE book
{
bookid varchar(20),pk
bkname varchar(30),
author varchar(20),
price varchar(10)
}
The SELECT Operation
private void GetBook()
{
//create DataContext object
OperationDataContext OdContext = new OperationDataContext();
var BookTable = from Book in OdContext.GetTable<Book>() select Book;
grdBook.DataSource = BookTable;
grdBook.DataBind();
}
The DELETE Operation
private void DeleteBook()
{
OperationDataContext OdContext = new OperationDataContext();
//Get Single Book which need to Delete
BOOK objBook= OdContext.BOOK.Single(Book => Book.Book_name == "java");
//Puts an entity from this table into a pending delete state and parameter is the entity which to be deleted.
OdContext.BOOK.DeleteOnSubmit(objBook);
// executes the appropriate commands to implement the changes to the database
OdContext.SubmitChanges();
}
private void GetBook()
{
//create DataContext object
OperationDataContext OdContext = new OperationDataContext();
var BookTable = from Book in OdContext.GetTable<Book>() select Book;
grdBook.DataSource = BookTable;
grdBook.DataBind();
}
The DELETE Operation
private void DeleteBook()
{
OperationDataContext OdContext = new OperationDataContext();
//Get Single Book which need to Delete
BOOK objBook= OdContext.BOOK.Single(Book => Book.Book_name == "java");
//Puts an entity from this table into a pending delete state and parameter is the entity which to be deleted.
OdContext.BOOK.DeleteOnSubmit(objBook);
// executes the appropriate commands to implement the changes to the database
OdContext.SubmitChanges();
}