我有这样的EF模型:
public class foo
{
private static TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);
testData tD;
public foo(testData localtD)
{
this.tD = localtD;
}
public String Name {
get { return tD.Name; }
set { }
}
public Double dueInDays {
get { return (indianTime - tD.StartDate).TotalDays; }
set { } }
}
我的API方法是
[Route("api/getSingleData")]
[HttpGet]
public dynamic getTestData()
{
var dataset2 = (from recordset in db.testDatas
select new foo(recordset)
);
return new { data = dataset2 };
}
在db.testDatas
我有日期字段,我想返回当前日期和开始日期之间的差异。
该数据库或多或少已完成,我无法对其进行更改以在db查询级别满足此要求。我为错误做的根本错误是什么
LINQ中仅支持无参数构造函数和初始值设定项 到实体
要弹出?
更重要的是,我应该如何修改代码以达到要求?
感谢。
答案 0 :(得分:0)
你无法做你想做的事。这就像你要得到的一样好......
public class foo
{
private static TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);
string _name;
DateTime _startDate;
public foo()
{
}
public String Name {
get { return _name; }
set { _name = value; }
}
public Double dueInDays
{
get { return (indianTime - _startDate).TotalDays; }
}
public DateTime StartDate
{
get { return _startDate; }
set { _startDate = value; }
}
}
API方法: -
[Route("api/getSingleData")]
[HttpGet]
public dynamic getTestData()
{
var dataset2 = (from recordset in db.testDatas
select new foo
{
Name = recordset.Name,
StartDate = recordset.StartDate
});
return new { data = dataset2 };
}
或者你可以按原样保留foo并做....
[Route("api/getSingleData")]
[HttpGet]
public dynamic getTestData()
{
var dataset2 = (from recordset in db.testDatas
select recordset
).ToList();
return new { data = dataset2.select(r => new foo(r); };
}
答案 1 :(得分:0)
您是否尝试过使用分部类来创建计算字段。类似的东西:
public partial class testDatas
{
public int duInDays
{
get
{
TimeZoneInfo INDIAN_ZONE = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);
return (indianTime - StartDate).TotalDays;
}
}
}