What is LINQ?
Language INtegrated Query
What does it do?
Lets you use SQL queries on Objects, mainly
Simple example
-Find all of the words the contain AR and put them in Alphabetical order.
Dim words() As String = {"DOG", "CARROT", "APPLE", "AARDVARK", "ELEPHANT"}
Dim TheLINQ_Matches As IEnumerable(Of String)
TheLINQ_Matches = From TheWord As String In words Where TheWord Like "*AR*" Order By TheWord Ascending
For Each Word As String In TheLINQ_Matches
Console.WriteLine(Word)
Next
It can be used on class objects too.
For example this one which is handles date events.
Public Class DatumEvent
Private mStartTime As DateTime
Private mJStart As Double
Public Property StartTime() As DateTime
Get
Return mStartTime
End Get
Set(ByVal value As DateTime)
mStartTime = value
mJStart = JDate(mStartTime)
End Set
End Property
Private mFinishTime As DateTime
Private mJFinish As Double
Public Property FinishTime() As DateTime
Get
Return mFinishTime
End Get
Set(ByVal value As DateTime)
mFinishTime = value
mJFinish = JDate(mFinishTime)
End Set
End Property
Public ReadOnly Property StartTime_Julian() As Double
Get
Return mJStart
End Get
End Property
Public ReadOnly Property FinishTime_Julian() As Double
Get
Return mJFinish
End Get
End Property
Public ReadOnly Property Delta_TimeSpan() As TimeSpan
Get
Return mFinishTime.Subtract(mStartTime)
End Get
End Property
Public ReadOnly Property Delta_Julian() As Double
Get
Return mJFinish - mJStart
'JDate(mFinishTime) - JDate(mStartTime)
End Get
End Property
Private mEventText As String
Public Property EventText() As String
Get
Return mEventText
End Get
Set(ByVal value As String)
mEventText = value
End Set
End Property
Private mEventColor As System.Drawing.Color
Public Property EventColor() As System.Drawing.Color
Get
Return mEventColor
End Get
Set(ByVal value As System.Drawing.Color)
mEventColor = value
End Set
End Property
Private Function JDate(ByRef thisdate As DateTime) As Double
Dim a As Double = Math.Floor((14 - thisdate.Month) / 14)
Dim y As Double = thisdate.Year + 4800 - a
Dim m As Double = thisdate.Month + (12 * a) - 3
Dim jdn As Double = 0
jdn = thisdate.Day + Math.Floor(((153 * m) + 2) / 5) + (365 * y) + Math.Floor(y / 4) - Math.Floor(y / 100) + Math.Floor(y / 400) - 32045
jdn += ((thisdate.Hour - 12) / 24) + (thisdate.Minute / 1440) + (thisdate.Second / 86400)
Return jdn
End Function
Public Sub New(ByRef Start As DateTime, ByRef Finish As DateTime, ByRef Text As String,ByRef color As System.Drawing.Color)
StartTime = Start
FinishTime = Finish
EventText = Text
EventColor = color
End Sub
End Class
Utilized by the following program.
Public Class Form1
Dim TheEvents As New List(Of DatumEvent)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TheEvents.Add(New DatumEvent(New Date(2008, 8, 24, 7, 0, 0), New Date(2008, 8, 24, 15, 0, 0), "Work Shift", System.Drawing.Color.Red))
TheEvents.Add(New DatumEvent(New Date(2008, 8, 26, 15, 0, 0), New Date(2008, 8, 26, 23, 0, 0), "Work Shift", System.Drawing.Color.DarkBlue))
TheEvents.Add(New DatumEvent(New Date(2008, 8, 30, 15, 0, 0), New Date(2008, 8, 30, 23, 0, 0), "Work Shift", System.Drawing.Color.Yellow))
TheEvents.Add(New DatumEvent(New Date(2008, 8, 31, 15, 0, 0), New Date(2008, 8, 31, 23, 0, 0), "Work Shift", System.Drawing.Color.Red))
TheEvents.Add(New DatumEvent(New Date(2008, 9, 1, 15, 0, 0), New Date(2008, 9, 1, 23, 0, 0), "Work Shift", System.Drawing.Color.Red))
TheEvents.Add(New DatumEvent(New Date(2008, 9, 2, 15, 0, 0), New Date(2008, 9, 2, 23, 0, 0), "Work Shift", System.Drawing.Color.Red))
DrawEvents(Me.PictureBox1, New Date(2008, 8, 23, 0, 0, 0), 1, 9)
DrawEvents(Me.PictureBox2, New Date(2008, 8, 25, 0, 0, 0), 1, 1)
End Sub
Public Sub DrawEvents(ByRef a As PictureBox, ByVal s As DateTime, ByVal DayWidth As Double, ByRef DayHeight As Double)
Dim l As New TimeSpan(DayHeight * DayWidth, 0, 0, 0)
Dim bmp As New Bitmap(a.Width, a.Height)
Dim HeightOfRow As Double = a.Height / DayHeight
Dim pg As Graphics = Graphics.FromImage(bmp)
pg.Clear(Color.White)
Dim FirstDatum As New DatumEvent(s, s, "", System.Drawing.Color.Black)
Dim VerticalOffset As Double = 0
Dim EventsMatches As IEnumerable(Of DatumEvent)
For i = 0 To DayHeight - 1
It even can handle complex searches like to one below.
Find all the events the that start or in progress between the start date & time and the finish date & time.
' Using LINQ find all Events Start and End DateTImes EventsMatches = From item In TheEvents _ Select item _ Where ((item.StartTime >= s) Or (item.StartTime.Add(item.Delta_TimeSpan) >= s)) _ And ((item.StartTime < s.AddDays(DayWidth)) Or (item.StartTime.Add(item.Delta_TimeSpan) < s.AddDays(DayWidth)))
All that in 1 Line of code.
' For Each Matching Evetn draw it
For Each MatchingEvent As DatumEvent In EventsMatches
DrawEvent(pg, FirstDatum, MatchingEvent, DayWidth, VerticalOffset, HeightOfRow, DayWidth * i)
Next
' Update Vertical offset
VerticalOffset += HeightOfRow
' Adjust Start date for next row
s = s.AddDays(DayWidth)
Next i
' Stick the created bitmap into it picture box
a.Image = bmp
End Sub
Finally the code that draws the event.
Public Sub DrawEvent( _
ByRef g As Graphics, _
ByVal TheDatumEvent As DatumEvent, _
ByRef ThisEvent As DatumEvent, _
ByVal DaysWide As Double, _
ByVal VertialOffset As Double, _
ByVal HeightOfRow As Double, _
ByVal DayWidthValue As Double _
)
Dim JulianStartOffset As Double = (ThisEvent.StartTime_Julian - TheDatumEvent.StartTime_Julian)
Dim JulianEventWidth As Double = JulianStartOffset + ThisEvent.Delta_Julian
Dim EventRect As New RectangleF( _
(JulianStartOffset - DayWidthValue) * (g.VisibleClipBounds.Width / DaysWide), _
VertialOffset, _
ThisEvent.Delta_Julian * (g.VisibleClipBounds.Width / DaysWide), _
HeightOfRow)
Using p As New Pen(ThisEvent.EventColor)
' Draw Rectangle
g.FillRectangle(p.Brush, EventRect.X, EventRect.Y, EventRect.Width, EventRect.Height)
' Draw the border
g.DrawRectangle(Pens.Black, EventRect.X, EventRect.Y, EventRect.Width, EventRect.Height)
End Using
End Sub
End Class
So Basics are
Dim Results As IEnumerable(Of <The Type or Class>)
Results = SELECT item WHERE <SQL STATEMENT>
Note item is very useful on Lists.
So now you too can be a LINQ using code-ninja
This post has been edited by AdamSpeight2008: 14 August 2012 - 10:21 AM





MultiQuote




|