2011-05-23 45 views
0

我有一个名为实体:它有一个属性ShowDuringRegistration为字节指定的强制转换无效铸造的int字节

与执行此行中我总是看到这个错误AttachmentType:指定强制转换无效,,不管那我发送一个字节作为参数

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(); 

,将这样 生成从 EmploymentRegistration中选择cast(count(*)as INT)col_0_0_ [AttachmentTyp e] attachment0_左外连接 EmploymentRegistration。 [EmploymentLicense] employment1_ on attachment0_.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); 
    } 

} 
+0

现在你已经介绍了'FindAll' - 那是什么?说实话,你的代码仍然很不明确,而'Tolist'的错误代替'ToList'意味着这不是你的代码。如果你可以展示一个简短但完整的例子,那真的会有所帮助。见http://tinyurl.com/so-hints。 – 2011-05-23 07:11:27

+0

根据迄今为止的讨论,AttachmentType的定义可能是罪魁祸首。如果你需要进一步的帮助,你真的需要发布堆栈跟踪。 – gazarsgo 2011-05-29 05:17:19

回答

3

你实际上并没有使用一个字节 - 你正在使用一个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; 
} 
+0

谢谢你,但它没有工作,因为我告诉,即使我通过一个字节,该方法不会工作 – Adrakadabra 2011-05-23 05:57:25

+0

@Adrakadabra:“它没有工作”是非常含糊。请给出它失败的确切细节,理想情况下也是堆栈跟踪。你说你*发送了一个字节,但没有显示任何代码......这并不清楚你的意思。 – 2011-05-23 06:01:14

+0

@Adrakadabra:请把它放到你的文章中,而不是作为评论。目前还不清楚为什么SQL有一个计数,你可以显示什么'AddExpression'呢? – 2011-05-23 06:42:36