首页 > 解决方案 > 如何在 c# 中创建扩展以返回可以在 linq 中转换为实体的新日期时间条目?

问题描述

所以我在 LinQ to entity 查询中有这个属性,我需要给它添加天数

isWithinNinetyDays = (a.ParcelData == null ? false :
    (a.ParcelData.Parcel_LetterTracking == null ? false : 
        (a.ParcelData.Parcel_LetterTracking.LMailDate == null ? false : 
            (System.Data.Entity.DbFunctions.AddDays(a.DateTimeEntry, 90) >= a.ParcelData.Parcel_LetterTracking.LMailDate.Value)))),

问题是我不想使用“System.Data.Entity.DbFunctions.AddDays(a.DateTimeEntry, 90)”,因为我将不得不这样做很多,而且每次都这么写太难看了。 ..是否可以在我可以打电话的地方进行分机

a.DateTimeEntry.MyExtension(90)
that will add 90 days and be able to convert it to a entity convertable to sql object? 

标签: c#entity-frameworkextension-methods

解决方案


使用using声明。

using System.Data.Entity;
DBFunctions.AddDays(a.DateTimeEntry, 90) ...

推荐阅读