LINQ常用技巧之二

 

              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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值