开发者

specified cast is not valid in casting int to byte

i have an entity named:AttachmentType which has a property ShowDuringRegistration as byte

with executing this line i always see this error :specified cast is not valid,, no matter that i'm sending a byte as parameter

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;
 }

even if i send a byte to WithAttachmentTypeShowDuringRegistration method and compare ShowDuringRegistration with that property it doesn't work

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;
}

here is when the error raises:

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=?

the inner exception :{"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.EmploymentLicens开发者_开发百科e);
    }
}`

by executing a simpler query like this:

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

that will be generated like this select cast(count(*) as INT) as col_0_0_ from EmploymentRegistration.[AttachmentType] attachment0_ left outer join EmploymentRegistration. [EmploymentLicense] employment1_ on attachment0_.EmploymentLicenseId=employment1_.EmploymentLicenseId where

attachment0_.ShowDuringRegistration=?

when i wanna know the number of returned value by

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

i'll see the error again

even by just executing this query the error would raise exactly like before:

//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);
    }

}


You're not actually using a byte - you're using an int. It normally works in C# because ShowDuringRegistration would be promoted to an int and then the comparison would be made. Try this instead:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜