LINQ - using linq in asp.net - Asked By Dilip Sharma on 10-Aug-11 06:37 AM

hi 

this is my code 
DataClassesDataContext db = new DataClassesDataContext();
     var r = from k in db.dds
         select k;
     GridView1.DataSource = r.ToList();
 
     GridView1.DataBind();
 
     dd tb = (dd)r.First();
     Response.Write(tb.name);




by this i get first row of my table.
if i want any other row from my table so..
 so where i can give row index...??


Reena Jain replied to Dilip Sharma on 10-Aug-11 06:46 AM
Hi,

Use data-context for this

The DataContext is the source of all entities mapped over a database connection. It tracks changes that you made to all retrieved entities and maintains an "identity cache" that guarantees that entities retrieved more than one time are represented by using the same object instance.

In general, a DataContext instance is designed to last for one "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.


public void FillGrid()
{
  DataClasses1DataContext dc = new DataClasses1DataContext();
 
  var q =
    from a in dc.GetTable<Order>()
    where a.CustomerID.StartsWith("A")
    select a;
 
  dataGridView1.DataSource = q;
}

Hope this will help you
Riley K replied to Dilip Sharma on 10-Aug-11 06:48 AM
LINQ has got a feature SKIP and TAKE which Skips the first few records
specifed and Take the next records specified

var query8 = CustomerList.Select((cust, index) => new { cust, index })
             .Where(c => c.cust.Country == "USA")
             .Select(c => new { c.cust.CustomerID, c.cust.CompanyName, c.index })
             .Skip(10)
             .Take(2);

Try and let me know

Dilip Sharma replied to Reena Jain on 10-Aug-11 06:52 AM
i know this...


i want  in my object whole data is come ...

and than i show in textbox 1 by 1 whn i click on next button...

just like this  

textbox1.text=ds.tables[0].rows[2][4];

sho i can show here direct 4 row...

how can i do this when i m using linq......


Jitendra Faye replied to Dilip Sharma on 10-Aug-11 07:01 AM
There are 2 solutions-

1.you can loop through the records, to move to next one...

var Query = from obj in context.tbl_name
              select obj;
       

        foreach (var result in Query)
        {
          //here you can fetch all the rows returned...
        }





2. You can try with Skip() method like this:

Dim products = From p In db.Products Select p
Dim pr As Product = products.Skip(4).First()


Hope it helps...

Dilip Sharma replied to Jitendra Faye on 10-Aug-11 07:03 AM
Thnks skip method is work....

Thnks to all...
Dilip Sharma replied to Riley K on 10-Aug-11 07:03 AM
thnkx...
Riley K replied to Dilip Sharma on 10-Aug-11 12:28 PM
Welcome