Linq中有时会出现无效操作异常

时间:2017-10-30 09:46:35

标签: c# .net linq invalidoperationexception

在制作时,我正在研究日志文件中的以下异常。 从XML Service获取它们时,会在某些记录中出现此异常。

我已经调查了所有可能的原因 https://msdn.microsoft.com/en-us/library/system.invalidoperationexception(v=vs.110).aspx

AR14=MATCH(AP14,October!$4:$4,0)    
AM16= IFERROR(AGGREGATE(15,6,ROW(October!$7:$33)/(INDEX(October!$7:$34,0,$AS$14)
<>""),ROWS(AM$16:AM16)),"") 
AN16=IFERROR(INDEX(October!$1:$1048576,$AM16,2),"") 
AO16=IFERROR(INDEX(October!$1:$1048576,$AM16,3),"") 
AP16=IFERROR(INDEX(October!$1:$1048576,$AM16,4),"") 
AQ16=IFERROR(INDEX(October!$1:$1048576,$AM16,5),"")

以下是我得到此异常的代码

 Error message: The cast to value type 'Boolean' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
    Stack trace    at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
       at System.Data.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling[TColumn](Int32 ordinal)
       at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
       at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
       at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
       at System.Linq.Queryable.Any[TSource](IQueryable`1 source)

在测试环境中没有发生异常,xml也看起来很好,所以它看起来像是一些代码linq问题。

请帮助

这是PTransaction类

  public bool Insert(PTransaction transaction)
        {
            try
            {
                using (var context = new DsModel())
                {
                    var query = from row in context.PTransactions
                        where
                            row.Id == transaction.Id
                            && row.Title == transaction.Title
                            && row.Author == transaction.Author
                            && row.BookType == transaction.BookType
                            && row.Imprint == transaction.Imprint
                            && row.PublicationDate == transaction.PublicationDate
                            && row.CreatedDateUTC == transaction.CreatedDateUTC.Date
                            //&& row.LastUpdatedDate == transaction.LastUpdatedDate.Date
                            && row.SupplierPriceBeforeDiscount == transaction.SupplierPriceBeforeDiscount
                            && row.SupplierPriceAfterDiscount == transaction.SupplierPriceAfterDiscount
                            && row.RetailerPriceBeforeDiscount == transaction.RetailerPriceBeforeDiscount
                            && row.RetailerPriceAfterDiscount == transaction.RetailerPriceAfterDiscount
                           // && row.RecommendedRetailPrice == transaction.RecommendedRetailPrice
                            && row.TransactionFee == transaction.TransactionFee
                            && row.CustomerId == transaction.CustomerId
                            && row.CustomerName == transaction.CustomerName
                            && row.IsLoan == transaction.IsLoan
                            && row.TransactionDateUtc == transaction.TransactionDateUtc
                        select row;

                    if (!query.Any()) //Guard if somehow it has run twice
                    {
                        transaction.kd_oprettet = DateUtil.Now();
                        context.PTransactions.Add(transaction);
                        context.SaveChanges();
                    }
                    else
                    {
                        Logger.Instance.Info("Transaction already exist with Identifier: " + transaction.Identifier);
                    }
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(string.Format(
                    "'{0}'.\nError message: {1}\nStack trace {2}",
                    transaction.Identifier, exception.Message, exception.StackTrace),exception);

                return false;
            }
            return true;
        }

请查看PLegalEntity类

    public partial class PTransaction
    {
        public int kd_id { get; set; }
        public System.DateTime kd_oprettet { get; set; }
        public string Identifier { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public string BookType { get; set; }
        public string Imprint { get; set; }
        public Nullable<System.DateTime> PublicationDate { get; set; }
        public System.DateTime CreatedDateUTC { get; set; }
        public System.DateTime LastUpdatedDate { get; set; }
        public decimal SupplierPriceBeforeDiscount { get; set; }
        public decimal SupplierPriceAfterDiscount { get; set; }
        public decimal RetailerPriceBeforeDiscount { get; set; }
        public decimal RetailerPriceAfterDiscount { get; set; }
        public Nullable<decimal> RecommendedRetailPrice { get; set; }
        public decimal TransactionFee { get; set; }
        public Nullable<int> CustomerId { get; set; }
        public string CustomerName { get; set; }
        public Nullable<bool> IsLoan { get; set; }
        public Nullable<System.DateTime> TransactionDate { get; set; }
        public Nullable<int> LegalEntityId { get; set; }
        public Nullable<System.DateTime> TransactionDateUtc { get; set; }

        public virtual PLegalEntity PLegalEntity { get; set; }
    }

0 个答案:

没有答案