Example 1 : การใช้ LINQ ในการ BETWEEN ด้วย String (ข้อความ)
Code (C#)
private void frmMain_Load(object sender, EventArgs e)
{
// Create new entities Object
using (var db = new myDatabaseEntities())
{
//Between string
string strFrom = "A";
string strTo = "D";
// Get data from CUSTOMER
var ds = (from c in db.CUSTOMER
where c.NAME.CompareTo(strFrom) >= 0 && c.NAME.CompareTo(strTo) <= 0
select c).ToList();
// if found item rows
if (ds.Count() > 0)
{
this.myDataGridView.DataSource = ds;
}
}
}
Code (VB.Net)
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Create new entities Object
Using db = New myDatabaseEntities()
'Between string
Dim strFrom As String = "A"
Dim strTo As String = "D"
' Get data from CUSTOMER
Dim ds = (From c In db.CUSTOMER
Where c.NAME.CompareTo(strFrom) >= 0 And c.NAME.CompareTo(strTo) <= 0
Select c).ToList()
' if found item rows
If ds.Count() > 0 Then
Me.myDataGridView.DataSource = ds
End If
End Using
End Sub
Screenshot
Example 2 : การใช้ LINQ ในการ BETWEEN ด้วย Number (ตัวเลข)
Code (C#)
private void frmMain_Load(object sender, EventArgs e)
{
// Create new entities Object
using (var db = new myDatabaseEntities())
{
//Between Number or Integer
int iFrom = 100000;
int iTo = 700000;
// Get data from CUSTOMER
var ds = (from c in db.CUSTOMER
where c.USED >= iFrom && c.USED <= iTo
select c).ToList();
// if found item rows
if (ds.Count() > 0)
{
this.myDataGridView.DataSource = ds;
}
}
}
Code (VB.Net)
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Create new entities Object
Using db = New myDatabaseEntities()
'Between Number or Integer
Dim iFrom As Integer = 100000
Dim iTo As Integer = 700000
' Get data from CUSTOMER
Dim ds = (From c In db.CUSTOMER
Where c.USED >= iFrom And c.USED <= iTo
Select c).ToList()
' if found item rows
If ds.Count() > 0 Then
Me.myDataGridView.DataSource = ds
End If
End Using
End Sub
Screenshot
Example 2 : การใช้ LINQ ในการ BETWEEN ด้วย DateTime (วันที่)
Code (C#)
private void frmMain_Load(object sender, EventArgs e)
{
// Create new entities Object
using (var db = new myDatabaseEntities())
{
//Between DateTime
DateTime dtFrom = Convert.ToDateTime("10-Aug-2015");
DateTime dtTo = Convert.ToDateTime("25-Aug-2015");
// Get data from AUDIT
var ds = (from c in db.AUDIT
where c.LOG_DATE >= dtFrom && c.LOG_DATE <= dtTo
select c).ToList();
// if found item rows
if (ds.Count() > 0)
{
this.myDataGridView.DataSource = ds;
}
}
}
Code (VB.Net)
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Create new entities Object
Using db = New myDatabaseEntities()
'Between DateTime
Dim dtFrom As DateTime = Convert.ToDateTime("10-Aug-2015")
Dim dtTo As DateTime = Convert.ToDateTime("25-Aug-2015")
' Get data from AUDIT
Dim ds = (From c In db.AUDIT
Where c.LOG_DATE >= dtFrom And c.LOG_DATE <= dtTo
Select c).ToList()
' if found item rows
If ds.Count() > 0 Then
Me.myDataGridView.DataSource = ds
End If
End Using
End Sub