Von SQL hab ich keine Ahnung, aber bei "Linq To Object" würde das so aussehen:
Public Class Form1
Private mFirmen As New List(Of Firma)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
mFirmen.Add(New Firma("Firma1", 10, 10))
mFirmen.Add(New Firma("Firma2", 20, 30))
mFirmen.Add(New Firma("Firma3", 30, 0))
mFirmen.Add(New Firma("Firma4", 40, 10))
mFirmen.Add(New Firma("Firma5", -10, 10))
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
Dim referenzFirma As Firma = New Firma("ReferenzFirma", 0, 0)
Dim firmenInNaehe = From f As Firma In mFirmen Where Entfernung( _
referenzFirma, f) <= 25 Select f
Dim dt As DataTable = FirmenToDataTable(referenzFirma, firmenInNaehe)
DataGridView1.DataSource = dt
End Sub
Public Function FirmenToDataTable(ByVal referenzFirma As Firma, ByVal _
firmen As IEnumerable(Of Firma)) As DataTable
Dim dt As New DataTable
dt.Columns.Add("Name", GetType(String))
dt.Columns.Add("Entfernung", GetType(Double))
dt.Columns.Add("X", GetType(Integer))
dt.Columns.Add("Y", GetType(Integer))
For Each f As Firma In firmen
Dim r As DataRow = dt.NewRow
r("Name") = f.Name
r("Entfernung") = Entfernung(referenzFirma, f)
r("X") = f.X
r("Y") = f.Y
dt.Rows.Add(r)
Next
Return dt
End Function
Public Function Entfernung(ByVal firmaA As Firma, ByVal firmaB As Firma) As _
Double
Return ((Math.Abs(firmaB.X - firmaA.X) ^ 2) + (Math.Abs(firmaB.Y - _
firmaA.Y) ^ 2)) ^ 0.5
End Function
End Class
Public Class Firma
Public Name As String
Public X As Integer
Public Y As Integer
Public Sub New(ByVal n As String, ByVal xValue As Integer, ByVal yValue As _
Integer)
Me.Name = n
Me.X = xValue
Me.Y = yValue
End Sub
End Class Die Klasse Firma stellt einen Datensatz dar.
Die Berechnung läuft ganz einfach über den Satz des Pythagoras.
Edit: Ein DataGridView wird i.d.R. an eine DataTable gebunden, nicht manuell befüllt.
Maas
Beitrag wurde zuletzt am 20.02.10 um 21:19:27 editiert. |