views:

87

answers:

0

Hello all.

I am following the good old Scott Gu LINQ to SQL guide to LINQ and have stumbled upon a query.

Here is my scenario. I have a textbox a client should populate with a number (tbxHowMany), two radio buttons that define a material (radPaper & radGlass). A also have a button (btnReturn) that when clicked, a gridview (gridview1) should be populated.

Now, I would just programmatically write the queries in the btnReturn click but unfortunately, becuase I am using take, this renders the paging and sorting worthless i.e. it doesn't work.

So my intention was to use a custom LINQ expressiona la Scott's guide as it would appear that pagain and sorting would continue to work due to the deferred execution.

Therefore I present to you the following code used in the LinqDatasourceSelectArgs:

protected void LQTOPReportDS_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{

    if (radPaper.Checked)
    {
    CompleteWeightsDataContext dbPa = new CompleteWeightsDataContext();
    int max = 0;
    if (int.TryParse(tbxHowMany.Text, out max))
    {
        var queryPa = dbPa.tblOnlineReportingCOMPLETEWeights
            .Where(x => x.MaterialLevel == "Primary" && x.MaterialText == "Paper")
            .OrderByDescending(x => x.ProductPercentage).Take(max);

        e.Result = queryPa;
      }

    else if (radGlass.Checked)
    {
        CompleteWeightsDataContext dbGl = new CompleteWeightsDataContext();
        int max = 0;
        if (int.TryParse(tbxHowMany.Text, out max))
        {
            var queryGl = dbGl.tblOnlineReportingCOMPLETEWeights
                .Where(x => x.MaterialLevel == "Primary" && x.MaterialText == "Glass")
                .OrderByDescending(x => x.ProductPercentage).Take(max);

            e.Result = queryGl;
        }
    }
    }
}

How do I go about using this method above so that when the user clicks btnReturn, the appropriate gridview is presented?

Apologies if the above doesn't make sense, I've been scratching my head for a few hours now.