C# 如何使用Linq to Sql指定DateTimeOffset精度
我有一个VS2010项目,其中包含从SQL 2008数据库生成的Linq到SQL类。相关表有一个专门创建的列,该列的类型为datetimeoffset(3),每天用作分区键:C# 如何使用Linq to Sql指定DateTimeOffset精度,c#,linq-to-sql,database-partitioning,C#,Linq To Sql,Database Partitioning,我有一个VS2010项目,其中包含从SQL 2008数据库生成的Linq到SQL类。相关表有一个专门创建的列,该列的类型为datetimeoffset(3),每天用作分区键: CREATE TABLE [dbo].[tableA]( [Id] [int] IDENTITY(1,1) NOT NULL, [Created] [datetimeoffset](3) NOT NULL, [A] [int] NULL, CONSTRAINT [PK_TableA] PRIMAR
CREATE TABLE [dbo].[tableA](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Created] [datetimeoffset](3) NOT NULL,
[A] [int] NULL,
CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED
(
[Created] ASC,
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) on partScheme_Daily_OnCreatedDate (Created)
) on partScheme_Daily_OnCreatedDate (Created)
为此列生成的linq to SQL代码将DateTimeOffset显示为其类型
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.TableA")]
public partial class TableA : INotifyPropertyChanging, INotifyPropertyChanged {
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _Id;
private System.DateTimeOffset _Created;
etc
我可以基于生成的类创建并执行linq到sql查询
var query = db.TableA.Where(pt => pt.Created >= (DateTimeOffset) startTime && pt.Created <= endTime
var query=db.TableA.Where(pt=>pt.Created>=(DateTimeOffset)startTime&&pt.Created根据需要使用格式
DateTimeOffset outputDate = new DateTimeOffset(2007, 11, 1, 9, 0, 0,
new TimeSpan(-7, 0, 0));
string format = "dddd, MMM dd yyyy HH:mm:ss zzz";
// Output date and time using custom format specification
Console.WriteLine(outputDate.ToString(format, null as DateTimeFormatInfo));
Console.WriteLine(outputDate.ToString(format, CultureInfo.InvariantCulture));
Console.WriteLine(outputDate.ToString(format,
new CultureInfo("fr-FR")));
Console.WriteLine(outputDate.ToString(format,
new CultureInfo("es-ES")));
// The example displays the following output to the console:
// Thursday, Nov 01 2007 09:00:00 -07:00
// Thursday, Nov 01 2007 09:00:00 -07:00
// jeudi, nov. 01 2007 09:00:00 -07:00
// jueves, nov 01 2007 09:00:00 -07:00
.Net使用7的刻度,您想使用3…折衷…忽略这个问题,看看他们的解决方案…感谢您的回答,我知道如何消除dotnet精度。但我希望看到的是linq到sql将dotnet DateTimeOffset转换为sql DateTimeOffset(3)而不是DateTimeOffset(7)的方法@Ako你有DBML文件吗?这有点过分了,但是你有没有考虑过创建自定义类型映射?谢谢Paul,我会检查一下。DBML中的类型是straigt forward:
DateTimeOffset outputDate = new DateTimeOffset(2007, 11, 1, 9, 0, 0,
new TimeSpan(-7, 0, 0));
string format = "dddd, MMM dd yyyy HH:mm:ss zzz";
// Output date and time using custom format specification
Console.WriteLine(outputDate.ToString(format, null as DateTimeFormatInfo));
Console.WriteLine(outputDate.ToString(format, CultureInfo.InvariantCulture));
Console.WriteLine(outputDate.ToString(format,
new CultureInfo("fr-FR")));
Console.WriteLine(outputDate.ToString(format,
new CultureInfo("es-ES")));
// The example displays the following output to the console:
// Thursday, Nov 01 2007 09:00:00 -07:00
// Thursday, Nov 01 2007 09:00:00 -07:00
// jeudi, nov. 01 2007 09:00:00 -07:00
// jueves, nov 01 2007 09:00:00 -07:00