[英]Entity Framework 4.0/ How to concatenate string from date and time
I have this table我有这张桌子
CREATE TABLE Receipt
(
id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
idcustom INT NOT NULL,
idstaff INT NOT NULL,
Daypayat DATE NOT NULL,
Timepayat TIME NOT NULL,
total INT NOT NULL
)
And I want to concatenate Daypayat with Timepayat like DateTime (dd/MM/yyyy hh:mm)我想将 Daypayat 与 Timepayat 像 DateTime (dd/MM/yyyy hh:mm) 连接起来
Firstly do the concatenation and save the result in the specific column in SQL using the following method.首先使用以下方法进行连接并将结果保存在SQL中的特定列中。
select cast(@date+' '+@time as datetime)
or you can do this to convert,或者你可以这样做来转换,
select cast(@date as datetime)+@time
and then use Datetime struct in C# to get result.然后在 C# 中使用 Datetime 结构来获取结果。
Formatting is a view / consumer concern, it should not be a domain one.格式化是一个视图/消费者关注的问题,它不应该是一个域的。 The entity should expose DayPayAt and TimePayAt as their respective types.实体应该公开 DayPayAt 和 TimePayAt 作为它们各自的类型。 (DateTime and DateTime / Timespan) When you query the data, project it to a ViewModel/DTO which can combine those values into a PayAt DateTime, then let your view format that how you desire. (日期时间和日期时间/时间跨度)当您查询数据时,将其投影到 ViewModel/DTO,它可以将这些值组合成一个 PayAt DateTime,然后让您的视图格式符合您的要求。
So if your entity has:因此,如果您的实体具有:
public DateTime DayPayAt { get; set; }
public Timespan TimePayAt { get; set; }
your query would be:您的查询将是:
var receipt = context.Receipts
.Where(/*conditions...*/)
.Select(x => new
{
// other fields or related data as needed.
x.Daypayat,
x.Timepayat
}).ToList() // Materialize our query...
.Select(x => new ReceiptViewModel
{
// Copy related data from results...
PayAt = x.DayPayAt.Add(x.Timepayat);
}).ToList();
Then in your view you can format "PayAt" using whatever custom or standard formatting string you want.然后在您的视图中,您可以使用您想要的任何自定义或标准格式字符串来格式化“PayAt”。 @Model.PayAt.ToString("g")
for instance would give you the standard short date/time format for your region, or use a custom format string for something specific. @Model.PayAt.ToString("g")
将为您提供您所在地区的标准短日期/时间格式,或者为特定内容使用自定义格式字符串。
The above example does a double-projection, one to get the raw fields of interest from our entity, the second to project to the view model.上面的例子做了一个双投影,一个从我们的实体中获取感兴趣的原始字段,第二个投影到视图模型。 This may not be necessary depending if EF can perform a DateTime.Add
.这可能不是必需的,具体取决于 EF 是否可以执行DateTime.Add
。 (I don't believe so) (我不相信)
If Timepayat in the entity maps to a DateTime field, (Ie 0000-00-00T14:20:00) then the PayAt conversion would be:如果实体中的 Timepayat 映射到 DateTime 字段(即 0000-00-00T14:20:00),则 PayAt 转换将是:
PayAt = x.DayPayAt.Add(x.Timepayat.TimeOfDay);
An alternative if you want to return the entity and handle it there is to use an unmapped property on the Receipt entity for PayAt that composes the DateTime.如果您想返回实体并处理它,另一种方法是在构成 DateTime 的 PayAt 的 Receipt 实体上使用未映射的属性。 The caveat of that approach is that you need to remember not to reference any unmapped property in a Linq expression since it cannot be translated to SQL.该方法的警告是您需要记住不要在 Linq 表达式中引用任何未映射的属性,因为它无法转换为 SQL。
public class Receipt
{
// Mapped properties...
public DateTime Daypayat { get; set; }
public TImespan Timepayat { get; set; }
[NotMapped]
public DateTime PayAt
{
get { return Daypayat.Add(Timepayat); }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.