【问题标题】:The binary operator LessThan is not defined for the types 'System.Nullable`1[System.DateTime]' and 'System.Nullable`1[System.DateTimeOffset]'没有为“System.Nullable`1[System.DateTime]”和“System.Nullable`1[System.DateTimeOffset]”类型定义二元运算符 LessThan
【发布时间】:2015-04-19 15:24:33
【问题描述】:

我正在尝试创建 Odata 端点,但每当我尝试执行任何涉及日期的查询时都会收到此错误。

我在下面的一个非常简单示例中重新创建了它。

数据库表

EDMX(片段)

<edmx:ConceptualModels>
  <Schema Namespace="DataWarehouseModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
    <EntityContainer Name="DataWarehouseEntities1" annotation:LazyLoadingEnabled="true">
      <EntitySet Name="People" EntityType="DataWarehouseModel.Person" />
    </EntityContainer>
    <EntityType Name="Person">
      <Key>
        <PropertyRef Name="ID" />
      </Key>
      <Property Name="ID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
      <Property Name="FirstName" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="true" />
      <Property Name="LastName" Type="String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="true" />
      <Property Name="DOB" Type="DateTime" Nullable="false" Precision="3" />
    </EntityType>
  </Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
  <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
    <EntityContainerMapping StorageEntityContainer="DataWarehouseModelStoreContainer" CdmEntityContainer="DataWarehouseEntities1">
      <EntitySetMapping Name="People">
        <EntityTypeMapping TypeName="DataWarehouseModel.Person">
          <MappingFragment StoreEntitySet="Person">
            <ScalarProperty Name="DOB" ColumnName="DOB" />
            <ScalarProperty Name="LastName" ColumnName="LastName" />
            <ScalarProperty Name="FirstName" ColumnName="FirstName" />
            <ScalarProperty Name="ID" ColumnName="ID" />
          </MappingFragment>
        </EntityTypeMapping>
      </EntitySetMapping>
    </EntityContainerMapping>
  </Mapping>
</edmx:Mappings>

型号

public partial class Person
{
    [Key]public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public System.DateTime DOB { get; set; }
}

控制器

public class PersonController : ODataController
{
    private DataWarehouseServiceContext db = new DataWarehouseServiceContext();

    private bool PersonExists(int key)
    {
        return db.Persons.Any(p => p.ID == key);
    }

    [EnableQuery]
    public IQueryable<Person> Get()
    {
        return db.Persons;
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
}

查询

http://localhost:53205/OData/Person?$top=20&$filter=DOB eq 1972-11-20T00:00:00.000Z
http://localhost:53205/OData/Person?$top=20&$filter=DOB lt 1972-11-20T00:00:00.000Z

等等

所有失败,消息如下:-

The query specified in the URI is not valid. The binary operator LessThan is not defined for the types 'System.Nullable`1[System.DateTime]' and 'System.Nullable`1[System.DateTimeOffset]'.

谁能帮忙?

【问题讨论】:

  • 试试网址http://localhost:53205/OData/Person?$top=20&amp;$filter=DOB+eq+datetime'1972-11-20'
  • 给出“在 URI 中指定的查询无效。无法识别 'Edm.String' 文字 'datetime'1972-11-20'' 在 '7' 在 'DOB eq datetime'1972-11 -20''”。 - 尝试了一些其他的排列,带引号和不带引号等。

标签: .net asp.net-web-api odata


【解决方案1】:

这里的问题是,虽然 Microsoft 已为 partly 添加了 DateTime 支持 (see comments),但 OData V4 不包含 DateTime 作为原始类型。这意味着您的过滤器仍然需要使用 DateTimeOffset。

虽然不理想,但将 DateTime 转换为 DateTimeOffset 似乎可以解决问题。

/OData/Person?$top=20&$filter=cast(DOB,'Edm.DateTimeOffset') eq 1972-11-20T00:00:00.000Z

【讨论】:

  • 希望对您有所帮助。仅供参考:此问题正在github.com/OData/WebApi/issues/206 进行跟踪,并将在 2015 年 3 月 30 日的下一个 Web API 版本 (5.5) 中得到修复。
【解决方案2】:

出现此异常的原因是 Odata 不支持 DateTime 类型,但现在 Web API OData v4 在版本 5.5 中支持它。不要忘记设置 SetTimeZoneInfo:

config.SetTimeZoneInfo(TimeZoneInfo.Utc);

默认情况下,dateTime 属性的时区将被视为本地时区。

更多信息:ASP.NET Web API for OData V4 Docs DateTime support

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多