Search code examples
linq-to-sqlc#-3.0linqdatasource

LinqDataSource query help


I know LINQ doesn't have the SQL IN clause but rather uses "contains". Can I use this on a LinqDataSource? I want to write a simple query that is equivelent to this: SELECT * FROM tableA WHERE tableA.requestType NOT IN (5,6,7,8) AND tableA.someBitField IS NULL.

Is this possible using the LinqDataSource out of the box? Thanks for any pointers.

Cheers, ~ck in San Diego


Solution

  • Yes, quite possible. Just handle the Selecting event on the datasource. The LinqDataSoruce class page on MSDN contains a great example already. Modifying that:

    public partial class Default3 : System.Web.UI.Page
    {
        int[] validRequestTypes = { 5, 6, 7, 8 };
    
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    
        protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            using(var dc = new MyDataContext())
            {
                var qry = from item in dc.tableAs 
                          where validRequestTypes.contains(item.requestType)
                             && item.someBitField == null
                          select item;
                e.Result = qry;
            }
        }
    }