Test Menu

  • Commentary
  • Deals
  • Coupons

May 23, 2011

Linq - Select TOP Example

An example of a linq statement that takes the first three items in the collection of listitems.  I used this example when I was synchronizing different tabs of a website that each had listboxes.  One of the tabs had a limit of three selected items:

Protected Sub Page_ListSync(ByVal lstSelected As List(Of ListItem), ByVal lstAvailable As List(Of ListItem))

        Try

            '  just take the first three from selected
            Dim liCollection = From li In lstSelected Select li Take 3

            lstSelected = liCollection.ToList()

            SRBC.RWQMN.SyncListboxes(lstSelectedStations, lstSelected, lstAvailableStations, lstAvailable)

        Catch ex As Exception
            SRBC.SRBCErrorLogging.LogError(ex, "ctrlStatistics.Page_ListSync")
        End Try

End Sub

May 4, 2011

SQL - Keep Functions Out of the Where Clause

Taking functions out of the where clause improved stored procedure performance by 2300%.  In my original stored procedure with two functions in the where clause it used to take 46 seconds to complete, but by replacing the functions with the raw data value that they return, the time was reduced to just 2 seconds

Slow:


SELECT MAX(Temperature) 
FROM #tblStationStatistics 
WHERE StationID = A.StationID
    AND Temperature BETWEEN dbo.fnGetMinThreshold('Temperature') AND dbo.fnGetMaxThreshold('Temperature')


Fast:


SELECT MAX(Temperature) 
FROM #tblStationStatistics 
WHERE StationID = A.StationID
    AND Temperature BETWEEN -10 AND 60

SQL - Check if a Temp Table Exists

IF OBJECT_ID('tempdb..#tblRawData') IS NOT NULL
    BEGIN
        TRUNCATE TABLE  #tblRawData
    END