none
VB Equiv to programmattically adding Where clauses in LINQ? RRS feed

  • Question

  • Is there a VB equivalent to the following C# LINQ code that programmatically adds Where clauses to a LINQ query?

     

     

    Code Snippet

    var q = from nr in TestDatabase.NumericResults 
            select nr;

     

    // Check by single date

    if ( chkByDate.Checked )

    {

    q = q.Where(x => x.Date = d1)

    }

     

    // Check by date range

    if ( chkByDateRange.Checked )

    {

    q = q.Where(x => x.Date >= d1);

    q = q.Where(x => x.Date <= d2);

     }

     

    return q.ToList();

     

     

    I'm trying to use GUI controls to filter the search results in a VB 2008 project.

     

    Thanks - Mark

    Thursday, December 4, 2008 7:30 PM

Answers

  • Found the answer in post:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3257065&SiteID=1

     

    Apparently queries are "composable" where you can modify them dynamically.  The following code works for me in VB (and I'm really grateful for it!)

     

    Code Snippet

    Dim Step_Data = _
       From nr In db.MTD_NumericResultDatas _
       Select nr

     

    ' Modify query based on filter controls.
    If chkUseDateRange.Checked Then
       Step_Data = _
          From nr In Step_Data _
          Where _
             nr.START >= dtDate_From.Value _
             And nr.START <= dtDate_To.Value _
          Select nr
    Else
       Step_Data = _
          From nr In Step_Data _
          Where _
             nr.START = dtDate_From.Value _
          Select nr
    End If

     

     

    - Mark

    Thursday, December 4, 2008 8:47 PM

All replies

  • Found the answer in post:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3257065&SiteID=1

     

    Apparently queries are "composable" where you can modify them dynamically.  The following code works for me in VB (and I'm really grateful for it!)

     

    Code Snippet

    Dim Step_Data = _
       From nr In db.MTD_NumericResultDatas _
       Select nr

     

    ' Modify query based on filter controls.
    If chkUseDateRange.Checked Then
       Step_Data = _
          From nr In Step_Data _
          Where _
             nr.START >= dtDate_From.Value _
             And nr.START <= dtDate_To.Value _
          Select nr
    Else
       Step_Data = _
          From nr In Step_Data _
          Where _
             nr.START = dtDate_From.Value _
          Select nr
    End If

     

     

    - Mark

    Thursday, December 4, 2008 8:47 PM
  • Yeah, I prefer the following approach. 


    Code Snippet

       dim q = (from nr as NumericResult in TestDatabase.NumericResults _
                Select nr)
       
       if checkByDate.Checked then
            q = q.where(function(nr as NumericResult) nr.date = d1)
       end if

       if chkByDateRange.Checked Then
            q = q.where(function(nr as NumericResult) nr.date >= d1)
            q = q.where(function(nr as numericresult) nr.date <= d2)
       end if

       return q.tolist()


    Wednesday, December 10, 2008 12:04 PM