我有一個名爲實體:它有一個屬性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);
}
}
現在你已經介紹了'FindAll' - 那是什麼?說實話,你的代碼仍然很不明確,而'Tolist'的錯誤代替'ToList'意味着這不是你的代碼。如果你可以展示一個簡短但完整的例子,那真的會有所幫助。見http://tinyurl.com/so-hints。 – 2011-05-23 07:11:27
根據迄今爲止的討論,AttachmentType的定義可能是罪魁禍首。如果你需要進一步的幫助,你真的需要發佈堆棧跟蹤。 – gazarsgo 2011-05-29 05:17:19