运行linq 2 sql语句时出现InvalidCastException

时间:2010-12-17 11:28:16

标签: c# linq-to-sql

C#,Linq2Sql,.NET 2.0,SQL 2005 Express

当我运行以下代码时,我得到了一个 InvalidCastException无法将System.Byte []类型强制转换为System.String

这不会发生在表中的所有行上,即此代码运行正常。

我检查表中没有linq声明为SYstem.Byte []

的列

堆栈跟踪表示在linq语句和I11时发生异常 在代码中,GetProject永远不会被调用,此异常发生在此方法之前 被称为。

所以我认为这是Linq框架错误。

这让我难过?任何想法?

[代码]

    public Project GetProject(int id)
    {
        var project = (from row in _dbctx.Projects
                       where row.ProjectID == id
                       select row).SingleOrDefault();

        Project proj = GetProject(project);

        return proj;
    }

    private Project GetProject(TBRServices.Domain.TBR.DataContext.Project sqlproject)
    {
        Project project = new Project();

        project.ProjectID = sqlproject.ProjectID;
        project.ProjectName = sqlproject.ProjectName;
        project.ProjectDescription = sqlproject.ProjectDescription;
        project.ClientID = sqlproject.ClientID;
        project.PurchaseOrderNumber = sqlproject.PurchaseOrderNumber;
        project.ProjectTotalBillingEstimate = sqlproject.ProjectTotalBillingEstimate;
        project.EmployeeID = sqlproject.EmployeeID;
        project.ProjectBeginDate = sqlproject.ProjectBeginDate.ToString();
        project.ProjectEndDate = sqlproject.ProjectEndDate.ToString();
        project.Active = sqlproject.Active;
        project.InclGST = sqlproject.InclGST;
        project.ContractRate = sqlproject.ContractRate;
        project.MarginRate = sqlproject.MarginRate;
        project.Notes = sqlproject.Notes;
        project.PayrollTax = sqlproject.PayrollTax;
        project.EmployeeReference = sqlproject.EmployeeReference;
        project.PayReference = sqlproject.PayReference;
        project.ProjectLocation = sqlproject.ProjectLocation;
        project.ContractTypeID = sqlproject.ContractTypeID;
        project.DoNotCalcMarginOrGST = sqlproject.DoNotCalcMarginOrGST;
        project.ConvertToInvoice = sqlproject.ConvertToInvoice;
        project.AuthFirstname = sqlproject.AuthFirstname;
        project.AuthLastname = sqlproject.AuthLastname;
        project.AuthEmail = sqlproject.AuthEmail;
        project.CapturedDocumentType = sqlproject.CapturedDocumentType;
        project.TimesheetProcessingType = sqlproject.TimesheetProcessingType;
        project.TimesheetFrequency = sqlproject.TimesheetFrequency;
        project.AggregateTimesheet = sqlproject.AggregateTimesheet;
        project.AggregateAutomatically = sqlproject.AggregateAutomatically;
        project.BusProcEmpCut = sqlproject.BusProcEmpCut;
        project.BusProcAgencyCut = sqlproject.BusProcAgencyCut;
        project.SendCopyTimesheet = sqlproject.SendCopyTimesheet;
        project.TimesheetRecipient1 = sqlproject.TimesheetRecipient1;
        project.TimesheetRecipient2 = sqlproject.TimesheetRecipient2;
        project.SendCopyInvoice = sqlproject.SendCopyInvoice;
        project.PublishedOn = sqlproject.PublishedOn.ToString();
        project.SendAggTimesheetsWhenReceived = sqlproject.SendAggTimesheetsWhenReceived;
        project.AuthoriseInvoice = sqlproject.AuthoriseInvoice;
        project.HourlyDailyMthly = sqlproject.HourlyDailyMthly;
        project.EmployeeCategory = sqlproject.EmployeeCategory;
        project.AgencyRate = sqlproject.AgencyRate;
        project.ConsultantID = sqlproject.ConsultantID;
        project.SendCopyInvoiceEmployee = sqlproject.SendCopyInvoiceEmployee;
        project.UseClientDetailsForTimesheet = sqlproject.UseClientDetailsForTimesheet;
        project.FaxedTimesheet = sqlproject.FaxedTimesheet;
        project.TimesheetEditable = sqlproject.TimesheetEditable;
        project.IncludeInReport = sqlproject.IncludeInReport;
        project.SendLogonEmail = sqlproject.SendLogonEmail;
        project.NoticePeriodID = sqlproject.NoticePeriodID;
        project.ManagerCanViewPaidUnpaid = sqlproject.ManagerCanViewPaidUnpaid;
        project.LesterSalesStaff = sqlproject.LesterSalesStaff;
        project.IncludeSalesReport = sqlproject.IncludeSalesReport;
        project.CurrencyAccount = sqlproject.CurrencyAccount;
        project.LeaveMustBeAuthorisedTimesheet = sqlproject.LeaveMustBeAuthorisedTimesheet;
        project.LeaveMustBeAuthorisedLeaveForm = sqlproject.LeaveMustBeAuthorisedLeaveForm;
        project.DefaultHoursWorkedWeek = sqlproject.DefaultHoursWorkedWeek;
        project.DefaultHoursWorkedDay = sqlproject.DefaultHoursWorkedDay;
        project.AccruedLeaveStartDate = sqlproject.AccruedLeaveStartDate.ToString();
        project.AllowLeaveMoreThanAccrued = sqlproject.AllowLeaveMoreThanAccrued;
        project.IsPaidASalary = sqlproject.IsPaidASalary;
        project.AgencyID = sqlproject.AgencyID;
        project.SendEmailToManagerToOKLeave = sqlproject.SendEmailToManagerToOKLeave;
        project.SpecialPayrollNotes = sqlproject.SpecialPayrollNotes;
        project.SpecialInvoicingNotes = sqlproject.SpecialInvoicingNotes;
        project.InvoiceTermsMessage = sqlproject.InvoiceTermsMessage;
        project.AuthManagerPhone = sqlproject.AuthManagerPhone;
        project.LMF = sqlproject.LMF;
        project.LMFPercent = sqlproject.LMFPercent;
        project.LMFInclInPayRate = sqlproject.LMFInclInPayRate;
        project.LMFOnTop = sqlproject.LMFOnTop;
        project.AgencyComm = sqlproject.AgencyComm;
        project.AgencyCommPercent = sqlproject.AgencyCommPercent;
        project.AgencyCommInclInPayRate = sqlproject.AgencyCommInclInPayRate;
        project.AgencyCommOnTop = sqlproject.AgencyCommOnTop;
        project.PayRateInclMargin = sqlproject.PayRateInclMargin;
        project.MarginRatePercent = sqlproject.MarginRatePercent;
        project.MarginRateGST = sqlproject.MarginRateGST;
        project.MarginRateInclInPayRate = sqlproject.MarginRateInclInPayRate;
        project.MarginRateOnTop = sqlproject.MarginRateOnTop;
        project.PayRateInclSuper = sqlproject.PayRateInclSuper;
        project.PayrollTaxAmount = sqlproject.PayrollTaxAmount;
        project.PayrollTaxPercent = sqlproject.PayrollTaxPercent;
        project.PayrollTaxInclInPayRate = sqlproject.PayrollTaxInclInPayRate;
        project.PayrollTaxOnTop = sqlproject.PayrollTaxOnTop;
        project.TypeOfArrangement = sqlproject.TypeOfArrangement;
        project.ParameterRequired = sqlproject.ParameterRequired;
        project.SpecialPayrollEnabled = sqlproject.SpecialPayrollEnabled;
        project.ClientContactUsedReceiptOfInvoiceID = sqlproject.ClientContactUsedReceiptOfInvoiceID;
        project.ExcludeEmpNameOnInvoice = sqlproject.ExcludeEmpNameOnInvoice;
        project.BPForReporting = sqlproject.BPForReporting;
        project.UseGemteqInvoiceFormat = sqlproject.UseGemteqInvoiceFormat;
        project.TreatPayrollDetailsSameAsBP = sqlproject.TreatPayrollDetailsSameAsBP;


        return project;
    }

[/代码]

2 个答案:

答案 0 :(得分:0)

  

无法将System.Byte []类型强制转换为System.String

表示sqlproject中的属性存储为byte[],而project中的相应属性为string

答案 1 :(得分:0)

检查类似SO问题的答案。它不是确切的答案,但可以帮助您在查询中找到问题 -

Problem with Linq to SQL