tags:

views:

50

answers:

3

Hello all, I am creating a gridview that will be populated based upon a linq statement, the sql is as follows:

SELECT     TOP 10 IDDesc, UnitUserfield1, UnitUserfield2, ProductPercentage
FROM         tblOnlineReportingCOMPLETEWeights
WHERE     (MaterialLevel = 'Primary') AND (MaterialText = 'Paper')
ORDER BY ProductPercentage DESC

Now, what I would like to do is let the user specify the Top 10, so essentially it is a "Top x" this being defined in a textbox i.e. they type in 50 into the textbox, the linq query is executed and the gridview displays the top 50.

I understand that using Take is the area I want to look at, is this correct? Is this even possible?!

Any thoughts, muchly appreciated.

PS: apologies for asking thick questions, I am very new to all of this!

A: 

You are correct. Take user input and feed it to Take. That'll do.

int howMany = Convert.ToInt32 (HowManyTextBox.Value);

var queryResult = /*.....*/.Take (howMany);
Developer Art
Excellent, now I'll try and change my sql to linq...I may be sometime!!!
MrDean
Mark Byers
A: 
int max = 0;

if (Int.TryParse(TextBox1.Text, out max)
{
    var q = (from tbl where ... orderby ... desc).Take(max);
}

Along those lines

Nick Allen - Tungle139
A: 

Thank you all so much, I went with the following:

{
    ORWeightsDataClassesDataContext db = new ORWeightsDataClassesDataContext();
    int max = 0;
        if (int.TryParse(txtbxHowMany.Text, out max))
        {
    var queryV = db.tblOnlineReportingCOMPLETEWeights
                .Where(x => x.MaterialLevel == "Primary" && x.MaterialText == "Paper")
                .OrderByDescending(x => x.ProductPercentage).Take(max);

    GridView1.DataSource = queryV;
    GridView1.DataBind();
        }
}

It works a treat.

Thank so so much, very grateful and now my site is finally coming together...I feel liek celebrating...pub anyone?!

MrDean