ASp.Net day

Micro blog



About Satalaj

www.satalaj.com

The best inline translator

Live lookup to see what asp.net developers are searching





Linq to SQL       by Satalaj 22. May 2009 09:46
    


Referring to http://www.revenmerchantservices.com/page/Linq-to-Sql.aspx

Filter Items into DB using Linq to SQl


Create DataBaseContext objct
AdventureWorksDataContext advDBc = new AdventureWorksDataContext();

var query = from entities in advDBc.Products   // select entities from product

where entities.Name.StartsWith("B")       // This will give all products starting with B

select entities;    // all linq statements must end with select or groupby clauses

GridView1.DataSource = query;

GridView1.DataBind();

 

     

Linq to SQL Insert syntex       by Satalaj 22. May 2009 09:37
    


Referring to http://www.revenmerchantservices.com/page/Linq-to-Sql.aspx

Insert new Item into DB using Linq to SQl

First we will add Locations in to .DBML file. Drag and drop the Locations table into .DBML file

and perform below action to insert new location and show it on grid.

AdventureWorksDataContext dbc = new AdventureWorksDataContext();    // Create an object of Database context

Location locationinfo = new Location();   // Create entity of Location

locationinfo.Name = "Pune";    // Populate entity with Names,and other field

locationinfo.Availability = 1.5M;

locationinfo.CostRate = 1.6M;

locationinfo.ModifiedDate = DateTime.Now;

dbc.Locations.InsertOnSubmit(locationinfo);    // Pass this location entity to Database context for insertation

dbc.SubmitChanges();     // update changes into SQL

var query = from locationEntity in dbc.Locations     // Query the locations table for newly inserted field locations

where locationEntity.Name == "Pune"

select locationEntity;

GridView1.DataSource = query;     // Bind the query to Grid

GridView1.DataBind();

- Satalaj

 

     

learn Linq step by step       by Satalaj 21. May 2009 12:43
    


Videos are available at http://www.asp.net/learn/linq-videos/

All 7 videos are step by step


Linq is language integrated query.
Below is simple examle of Linq syntex

We will query an array of string.

Create an array of Strings
string[] arr = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".Split(",".ToCharArray());

//Populate listbox with array of strings

ListBox1.DataSource = arr;

//Bind array of strings to Listbox

ListBox1.DataBind();

//Using linq syntex we will query an array of strings and find the word "S"  and "M"

var
query = from alphabet in
arr

                    where (alphabet == "S" || alphabet == "M")

                    select alphabet  ; 
                   // A query body must end with a select clause or a group clause 

foreach(var alpha in query)

{

Response.Write(alpha+"<Br/>");   //Write the word found

}


 


How simple it is !