EF(Linq)框架使用过程中的小技巧汇总 dbfunctions

这篇博客总结本人在实际项目中遇到的一些关于EF或者Linq的问题,作为以后复习的笔记或者供后来人参考(遇到问题便更新)。

目录


技巧1: DbFunctions.TruncateTime()的使用

有没有遇到做这样的错误:

  • LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this method cannot be translated into a store expression.【LINQ to Entities不能识别‘System.String ToShortDateString()’方法】
  • System.NotSupportedException: The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.【LINQ to Entities不支持类型成员‘Date’,仅支持初始化器,实体成员和实体导航属性】
var query = from order in _orderRepository.GetAll()
                .Where(o => o.OrderType == OrderType.LineSold)
                .WhereIf(input.OrderDate!=DateTime.MinValue,o=>o.OrderDate.Date==input.OrderDate.Date)
                .WhereIf(!string.IsNullOrEmpty(input.OrderNo),o=>o.OrderNo==input.OrderNo||o.OrderNo.EndsWith(input.OrderNo))
                .WhereIf(input.Status!=-1,o=>o.Status==input.Status)
    join device in _deviceRepository.GetAll()
            .WhereIf(!string.IsNullOrEmpty(input.Code),d=>d.Code==input.Code) on order.TerminalID equals device.Id
    join trans in _transDetailRepository.GetAll()
            .WhereIf(!string.IsNullOrEmpty(input.PayOrderNo),t=>t.PayOrderNo==input.PayOrderNo||t.PayOrderNo.EndsWith(input.PayOrderNo))
            on order.OrderNo equals trans.OrderNo into leftJoinResults
    from leftJoinResult in leftJoinResults.DefaultIfEmpty( )

    select new LineSoldOrderOutput
    {
        Code = device.Code,
        Id = order.Id,
        Amount = order.PayFee,
        OrderDate = order.OrderDate,
        OrderNo = order.OrderNo,
        PayOrderNo = leftJoinResult.PayOrderNo??"还没产生支付方订单号",
        Status = order.Status
    };

为啥有这种需求?
因为数据库中的DateTime类型都是以2016-03-11 11:25:59这种形式保存的,而我在客户端查询数据时,只要传入日期就行,不需要传入时间部分,所以必须把时间部分咔嚓掉(剪掉)。

正如上面的代码第三行,一开始是那么写的,结果报上面的第二种错误。
第二次改成了.WhereIf(input.OrderDate!=DateTime.MinValue,o=>o.OrderDate.ToShortDateString()==input.OrderDate.ToShortDateString()),结果报上面的第一种错误。

解决办法

.WhereIf(input.OrderDate!=DateTime.MinValue,o=>DbFunctions.TruncateTime(o.OrderDate)【我用的是EF6,Truncate,翻译为截断,该函数顾名思义也就是把时间部分去掉,只保留日期部分】
EF6以前你可能需要用EntityFunctions.TruncateTime(p.date) == dateWithoutTime

其他重点
我上面的代码还有使用linq进行三张表的连接,更重要的是,前两张表是内连接,之后再进行左连接。不熟悉linq语法的可以学习一下。

技巧2: Linq中对Datetime字段按照年月分组以及DbFunctions.CreateDateTime()的使用

有时候,在处理数据的时候,需要对数据进行分组,而且是对Datetime类型的字段按照年月进行分组,下面分别使用Linq的方法语法和查询语法进行分组:

方法语法

dateIncomeDtos = query
    .Where(o => o.OrderDate >= input.Start && o.OrderDate < DbFunctions.AddMonths(input.End,1))
    .OrderBy(o => o.OrderDate)
    .GroupBy(o => DbFunctions.CreateDateTime(o.OrderDate.Year, o.OrderDate.Month, 1, 0, 0, 0))
    .Select(group => new DateIncomeDto { Date = group.Key.Value, Income = group.Sum(item => item.PayFee ?? 0) });

查询语法

dateIncomeDtos = from q in query
    group q by new {date = new DateTime(q.OrderDate.Year, q.OrderDate.Month, 1)}
    into g
    select new DateIncomeDto
    {
        Date = g.Key.date
    };

方法语法使用的是Linq中提供的DbFunctions类中的CreateDateTime方法,给day的参数传入一个1至29中的整数,保证每个月中有这一天即可(我这里传入了1),这样,在分组的时候,EF就会将数据库中的每条记录的OrderDate字段的年和月进行分组。
查询语法思想是一样的,只不过用到了匿名类而已。

原文地址:https://www.cnblogs.com/sjqq/p/7630206.html