如何解决指定的强制转换在将int强制转换为字节时无效
| 我有一个名为: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(*)作为INT)作为col_0_0_
EmploymentRegistration。[AttachmentType]附件0_左外部联接
就业登记。 [EmploymentLicense]就业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);
}
}
解决方法
您实际上并没有使用字节-您正在使用
int
。它通常在C#中工作,因为将ѭ9提升为int
,然后进行比较。尝试以下方法:
public AttachmentSearchSpecification WithAttachmentTypeShowDuringRegistration
(bool showDuringRegistration=false)
{
byte value = showDuringRegistration ? (byte) 1 : (byte) 0;
AddExpression(p => p.AttachmentType.ShowDuringRegistration == value);
return this;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。