没有为类型'System.Nullable'1 [System.DateTime]'和'System.Nullable'1 [System.DateTimeOffset]'定义二进制运算符LessThan [英] The binary operator LessThan is not defined for the types 'System.Nullable`1[System.DateTime]' and 'System.Nullable`1[System.DateTimeOffset]'

查看:209
本文介绍了没有为类型'System.Nullable'1 [System.DateTime]'和'System.Nullable'1 [System.DateTimeOffset]'定义二进制运算符LessThan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create and Odata endpoint but I'm getting this error whenever I try and perform any queries involving dates.

我已经在下面的非常简单示例中重新创建了它.

I've re-created it in a very simple example below.

数据库表

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

全部失败,并带有以下消息:-

All fail with messages along the lines of :-

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]'.

任何人都可以帮忙吗?

推荐答案

此处的问题是,尽管Microsoft具有查看评论 ),OData V4不包括DateTime作为原始类型.这意味着您的过滤器仍然需要使用DateTimeOffset.

The issue here is that while Microsoft has partly added DateTime support (see comments), OData V4 doesn’t include DateTime as primitive type. This means that your filter still needs to use DateTimeOffset.

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

While not ideal, casting the DateTime to a DateTimeOffset seems to solve the problem.

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

这篇关于没有为类型'System.Nullable'1 [System.DateTime]'和'System.Nullable'1 [System.DateTimeOffset]'定义二进制运算符LessThan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆