Oshrib
Oshrib

Reputation: 1880

Query string on LINQ

I'm trying to do the next thing: i have the next code on c#:

b.Text = myDataContext.purchases.Count().ToString();

I have page for example: items.aspx?nID=144

How can i can I construct a linq query which will take the querystring parameter and check the value from the items table by the id (nID) that show on the address?

The table have the design for example: id, title, bla, main.

Upvotes: 1

Views: 2561

Answers (2)

VMAtm
VMAtm

Reputation: 28355

By Lambda expressions:

int nID;
string qs = QueryString["nID"];
if (Int32.TryParse(qs, nID)
    myDataContext.purchases.Where(p => p.id == nID);
    // Other options if nID is a primary key
    //myDataContext.purchases.FirstOrDefault(p => p.id == nID);
    //myDataContext.purchases.SingleOrDefault(p => p.id == nID);

By LINQ query:

int nID;
string qs = QueryString["nID"];
if (Int32.TryParse(qs, nID)
    var purchase = from p in myDataContext.purchases
        where p.id == nID
        select p;

Upvotes: 2

MonkeyCoder
MonkeyCoder

Reputation: 2620

If I understand you correctly, you are just trying to obtain the nID param from the QueryString and then reuse its value in some linq query to fetch the data from the database. You can achieve it with the code below (inside a ASPX page):

int nID;

string value = Request.QueryString["nID"];
if(int.TryParse(value, out nID))
{
    // your linq query
}

Upvotes: 0

Related Questions