【问题标题】:specified cast is not valid in casting int to byte指定的转换在将 int 转换为字节时无效
【发布时间】:2011-08-30 21:36:27
【问题描述】:

我有一个名为:AttachmentType 的实体,它有一个属性 ShowDuringRegistration 作为字节

执行此行时,我总是看到此错误:指定的转换无效,无论我发送一个字节作为参数

Repository<AttachmentType>.FindBySpecification(new AttachmentSearchSpecification()
    .WithTraceCodeOrNationalNumber(traceCodeString)
    .WithAttachmentTypeShowDuringRegistration(false))
    .Select(p=>new attachmentTypeModel()
     {
       Id=p.Id,
       Title=p.Title
     })
    .ToList();

 public AttachmentSearchSpecification WithAttachmentTypeShowDuringRegistration(bool showDuringRegistration=false)
 {          
      AddExpression(p => p.AttachmentType.ShowDuringRegistration == (showDuringRegistration ? 1 : 0));
      return this;
 }

即使我向 WithAttachmentTypeShowDuringRegistration 方法发送一个字节并将 ShowDuringRegistration 与该属性进行比较,它也不起作用

byte b=0;
Repository<AttachmentType>.FindBySpecification(new AttachmentSearchSpecification()
    .WithTraceCodeOrNationalNumber(traceCodeString)
    .WithAttachmentTypeShowDuringRegistration(b))
    .Select(p=> new attachmentTypeModel()
     {
       Id=p.Id,
       Title=p.Title
     })
    .ToList();

public AttachmentSearchSpecification WithAttachmentTypeShowDuringRegistration(byte showDuringRegistration)
{
    AddExpression(p => p.AttachmentType.ShowDuringRegistration == showDuringRegistration)
    return this;
}

这是引发错误的时间:

select cast(count(*) as INT) 
as col_0_0_ from EmploymentRegistration.[Attachment]
attachment0_, EmploymentRegistration.[Demand] demand1_,
 EmploymentRegistration.[AttachmentType] attachment5_ where ttachment0_.DemandId=demand1_.DemandId 
 and demand1_.PersonId=person3_.PartyId and person3_.PartyId=birthcerti4_.PersonId and
 attachment0_.AttachmentTypeId=attachment5_.AttachmentTypeId 
 and (demand1_.TraceCode like ('%'+?+'%') or birthcerti4_.NationalNumber like ('%'+?+'%'))
 and   attachment5_.ShowDuringRegistration=?

内部异常:{"Specified cast is not valid."}

protected void AddExpression(Expression<Func<T, bool>> expression);  this method get an experssion and append that expression to the linq query 



 public class AttachmentTypeMap : ClassMap<AttachmentType>
{
    public AttachmentTypeMap()
    {
        Schema("EmploymentRegistration");

        Id(p => p.Id);//int identity

        Map(p => p.Title);//string

        Map(p => p.ShowDuringRegistration);//byte

        Map(p => p.ScriptName)
            .Length(100);

        References(p => p.EmploymentLicense);
    }
}`

通过执行如下更简单的查询:

 Repository<AttachmentType>.FindAll().Where(p=>p.ShowDuringRegistration==(byte)1).Tolist();

将像这样生成 选择 cast(count(*) as INT) as col_0_0_ from EmployRegistration.[AttachmentType] attachment0_ 左外连接 就业登记。 [就业证]就业1_ 在附件 0_.EmploymentLicenseId=employment1_.EmploymentLicenseId 上
attachment0_.ShowDuringRegistration=?

当我想知道返回值的个数时

int _totalItems = Query.Count();  //Query is IQueryable<T>

我会再次看到错误

即使只是执行此查询,也会像以前一样引发错误:

//ShowDuringRegistration  is byte?
var data= Repository<AttachmentType>.Find(p => p.ShowDuringRegistration == 0)
                                    .ToList();
public interface IRepository<T> where T : class
{

   IQueryable<T> Find();

    IQueryable<T> Find(object id);

    IQueryable<T> FindBySpecification(ISpecification<T> specification);

    IQueryable<T> Find(Expression<Func<T, bool>> expression);

}


 public static class Repository<T> where T : class
{

private static IRepository<T> Current
    {
        get { return UnitOfWork.GetRepository<T>(); }
    }

 public static IQueryable<T> Find(Expression<Func<T, bool>> expression)
    {
        return Current.Find(expression);
    }

 public static IList<T> FindAll(Expression<Func<T, bool>> expression)
    {
        return Current.FindAll(expression);
    }

}

【问题讨论】:

  • 现在你已经介绍了FindAll - 那是什么?老实说,您的代码仍然很不清楚 - Tolist 而不是 ToList 的拼写错误表明这不是真的您的代码。如果你能展示一个简短但完整的例子,那真的很有帮助。见tinyurl.com/so-hints
  • 根据目前的讨论,可能是 AttachmentType 的定义问题。如果您需要进一步的帮助,您确实需要发布堆栈跟踪。

标签: c# .net linq


【解决方案1】:

您实际上并没有使用字节 - 您使用的是int。它通常在 C# 中工作,因为 ShowDuringRegistration 将被提升为 int 然后进行比较。试试这个:

public AttachmentSearchSpecification WithAttachmentTypeShowDuringRegistration
    (bool showDuringRegistration=false)
{
    byte value = showDuringRegistration ? (byte) 1 : (byte) 0;

    AddExpression(p => p.AttachmentType.ShowDuringRegistration == value);
    return this;
}

【讨论】:

  • 谢谢你,但它不起作用,正如我所说,即使我将一个字节传递给该方法它也不起作用
  • @Adrakadabra:“它没有用”非常含糊。请给出它失败的确切细节,理想情况下也是一个堆栈跟踪。你说你正在“发送”一个字节,但没有显示任何代码......不清楚你的意思。
  • @Adrakadabra:请将其放入您的帖子中,而不是作为评论。也不清楚为什么 SQL 有一个计数......你能展示一下 AddExpression 的作用吗?
  • @Adrakadabra:如果您删除所有其他要求会怎样?专注于演示问题的最小示例。
  • @Adrakadabra:我们仍然不知道Repository&lt;T&gt;.Find 长什么样。一个完整的程序会非常有用...
猜你喜欢
  • 2015-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 2016-03-04
相关资源
最近更新 更多