1、定义变量方式
Dim TotalAmount As Nullable(Of Decimal) = (Aggregate t In ModelContext.SomeItems Where (t.ForeignID = 123) Into Sum(CType(t.Price, Integer?)))
2、查看LINQ日志
MyDC.Log = Console.Out
3、Linq按时间查询
Public Function FindAll(ByVal pLedName As String, ByVal Sdate As Date, ByVal Edate As Date) As IEnumerable
Dim MyQuery = From myTbledOpentimed In MyDc.tbLedOpenTime Select myTbledOpentimed Where myTbledOpentimed.灯地址 = pLedName And myTbledOpentimed.开灯时间 >= Sdate.ToString And myTbledOpentimed.开灯时间 <= Edate.ToString
Return MyQuery
End Function
4、LINQ按一天进行统计查询
Dim MyDC = New DcTmpDataDataContext
Dim myqu = From t In MyDC.tbTmpdata Where t.日期 >= Dtp1.Value.ToString("yyyy-MM-dd 00:00:00") And t.日期 <= Dtp1.Value.ToString("yyyy-MM-dd 23:59:59") ' new { Into c1 = Sum(t.净重), c2 = Count(t.ID)}
DataGridView1.DataSource = myqu
5、统计一天的车数和产量
Dim MyQuery = Aggregate t In MyDC.tbTmpdata Where t.日期 >= Dtp1.Value.ToString("yyyy-MM-dd 00:00:00") And t.日期 <= Dtp1.Value.ToString("yyyy-MM-dd 23:59:59") Into c1 = Sum(t.净重), c2 = Count(t.ID)
6、日期计算
Console.WriteLine(Dtp1.Value.ToString("yyyy-MM-dd 00:00:00"))
Console.WriteLine(Dtp1.Value.Date.AddDays(1).ToString("yyyy-MM-dd 00:00:00"))
Dim dt1 As Date
dt1 = Now
Console.WriteLine(dt1.Date.AddDays(1))
7、按日期之间查询
var tb1=(From u in DBEntity.Users Select new {UserName=u.UserName,LastLoginDay=u.LastLoginTime }).ToList();
var tb2 = from u in tb1 select new {UserName=u.UserName,LastLoginDay=u.LastLoginTime.ToString("yyyy/MM/dd") }).ToList();
8、按日期查询
Dim myqu = From t In MyDC.tbTmpdata Where t.日期.Value.Year = Dtp1.Value.Year And t.日期.Value.Month = Dtp1.Value.Month And t.日期.Value.Day = Dtp1.Value.Day ' new { Into c1 = Sum(t.净重), c2 = Count(t.ID)}
DataGridView1.DataSource = myqu
MyDC.Log = Console.Out