我用这个d.PODate.ToString()
来显示(见图片)我可以使用inspect元素看到它...我想只显示日期而不是时间,当我尝试使用d.PODate.ToString("d")
时它给了它我这样的错误Method 'System.String ToString(System.String)' has no supported translation to SQL
和d.PODate.ToShortDateString()
相同的错误就像Method 'System.String ToShortDateString()' has no supported translation to SQL.
修改
这是我的GET方法
[HttpGet]
public Models.SysDataTablePager Get()
{
NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query);
string sEcho = nvc["sEcho"].ToString();
int iDisplayStart = Convert.ToInt32(nvc["iDisplayStart"]);
string sSearch = nvc["sSearch"].ToString();
int iSortCol = Convert.ToInt32(nvc["iSortCol_0"]);
string sSortDir = nvc["sSortDir_0"].ToString();
var BranchId = Convert.ToInt32(((wfmis.Global)HttpContext.Current.ApplicationInstance).CurrentBranchId);
var Count = db.TrnPurchaseOrders.Where(d => d.MstUser.Id == secure.GetCurrentUser() &&
d.MstBranch.Id == BranchId).Count();
var PurchaseOrders = from d in db.TrnPurchaseOrders
where d.MstBranch.Id == BranchId &&
d.MstBranch.MstUser.Id == secure.GetCurrentUser()
select new Models.TrnPurchaseOrder
{
Id = d.Id,
PeriodId = d.PeriodId,
Period = d.MstPeriod.Period,
BranchId = d.BranchId,
Branch = d.MstBranch.Branch,
PONumber = d.PONumber,
POManualNumber = d.POManualNumber,
//PODate = d.PODate.ToShortDateString(),
PODate = d.PODate.ToShortDateString(), //<------
SupplierId = d.SupplierId,
Supplier = d.MstArticle.Article,
TermId = d.TermId,
Term = d.MstTerm.Term,
RequestNumber = d.RequestNumber,
//DateNeeded = d.DateNeeded.ToShortDateString(),
DateNeeded = d.DateNeeded.ToString(), //<------
Particulars = d.Particulars,
RequestedById = d.RequestedById == null ? 0 :
d.RequestedById.Value,
RequestedBy = d.MstUser.FullName,
IsClosed = d.IsClosed,
PreparedById = d.PreparedById,
PreparedBy = d.MstUser.FullName,
CheckedById = d.CheckedById,
CheckedBy = d.MstUser1.FullName,
ApprovedById = d.ApprovedById,
ApprovedBy = d.MstUser2.FullName,
IsLocked = d.IsLocked,
CreatedById = d.CreatedById,
CreatedBy = d.MstUser3.FullName,
//CreatedDateTime = d.CreatedDateTime.ToShortDateString(),
CreatedDateTime = d.CreatedDateTime.ToString(),
UpdatedById = d.UpdatedById,
UpdatedBy = d.MstUser4.FullName,
//UpdatedDateTime = d.UpdatedDateTime.ToShortDateString()
UpdatedDateTime = d.UpdatedDateTime.ToString()
};
switch (iSortCol)
{
case 2:
if (sSortDir == "asc") PurchaseOrders = PurchaseOrders.OrderBy(d => d.PODate).Skip(iDisplayStart).Take(10);
else PurchaseOrders = PurchaseOrders.OrderByDescending(d => d.PONumber).Skip(iDisplayStart).Take(10);
break;
case 3:
if (sSortDir == "asc") PurchaseOrders = PurchaseOrders.OrderBy(d => d.PONumber).Skip(iDisplayStart).Take(10);
else PurchaseOrders = PurchaseOrders.OrderByDescending(d => d.PODate).Skip(iDisplayStart).Take(10);
break;
case 4:
if (sSortDir == "asc") PurchaseOrders = PurchaseOrders.OrderBy(d => d.Supplier).Skip(iDisplayStart).Take(10);
else PurchaseOrders = PurchaseOrders.OrderByDescending(d => d.Supplier).Skip(iDisplayStart).Take(10);
break;
default:
PurchaseOrders = PurchaseOrders.Skip(iDisplayStart).Take(10);
break;
}
var PurchaseOrderPaged = new Models.SysDataTablePager();
PurchaseOrderPaged.sEcho = sEcho;
PurchaseOrderPaged.iTotalRecords = Count;
PurchaseOrderPaged.iTotalDisplayRecords = Count;
PurchaseOrderPaged.TrnPurchaseOrderData = PurchaseOrders.ToList();
return PurchaseOrderPaged;
}
编辑2
我的数据库上的DateTime上有这个...
答案 0 :(得分:5)
d
来自数据库;它此时不作为.NET对象存在。您要求LINQ-to-SQL在数据库中执行.ToShortDateString(),但没有转换函数。
如果您先选择d
值并在查询结束时使用.ToList()
,则可以使用字符串格式化功能创建模型。
E.g:
// original db query, still returns IQueryable.
var dbPurchaseOrders = from d in db.TrnPurchaseOrders
where d.MstBranch.Id == BranchId &&
d.MstBranch.MstUser.Id == secure.GetCurrentUser();
// change db query to local, in-memory object list
var localPurchaseOrders = PurchaseOrders.ToList();
// change the local db.TrnPurchaseOrders objects into your Models.TrnPurchaseOrder objects.
// kept the d variable because I'm too lazy to change all lines of your code.
var PurchaseOrders = from d in localPurchaseOrders
select new Models.TrnPurchaseOrder
{
Id = d.Id,
PeriodId = d.PeriodId,
Period = d.MstPeriod.Period,
BranchId = d.BranchId,
Branch = d.MstBranch.Branch,
PONumber = d.PONumber,
POManualNumber = d.POManualNumber,
//PODate = d.PODate.ToShortDateString(),
PODate = d.PODate.ToShortDateString(), //<------
SupplierId = d.SupplierId,
Supplier = d.MstArticle.Article,
TermId = d.TermId,
Term = d.MstTerm.Term,
RequestNumber = d.RequestNumber,
//DateNeeded = d.DateNeeded.ToShortDateString(),
DateNeeded = d.DateNeeded.ToString(), //<------
Particulars = d.Particulars,
RequestedById = d.RequestedById == null ? 0 : d.RequestedById.Value,
RequestedBy = d.MstUser.FullName,
IsClosed = d.IsClosed,
PreparedById = d.PreparedById,
PreparedBy = d.MstUser.FullName,
CheckedById = d.CheckedById,
CheckedBy = d.MstUser1.FullName,
ApprovedById = d.ApprovedById,
ApprovedBy = d.MstUser2.FullName,
IsLocked = d.IsLocked,
CreatedById = d.CreatedById,
CreatedBy = d.MstUser3.FullName,
//CreatedDateTime = d.CreatedDateTime.ToShortDateString(),
CreatedDateTime = d.CreatedDateTime.ToString(),
UpdatedById = d.UpdatedById,
UpdatedBy = d.MstUser4.FullName,
//UpdatedDateTime = d.UpdatedDateTime.ToShortDateString()
UpdatedDateTime = d.UpdatedDateTime.ToString()
};
答案 1 :(得分:-1)
试试这个。
CreatedDateTime = DateTime.Parse(d.CreatedDateTime.ToString()).ToShortDateString();