2

我已经用谷歌搜索了这个错误并在这里阅读了一些关于堆栈溢出的帖子,但我仍然不明白这里的问题是什么。我理解“英语”,但不理解程序化推理。为什么我可以这样做:

 public void FillRegister(ItemMovementRegister register, IDateRange imqp)
        {
            var f = from detail in this.Context.ItemMovements
                    where (detail.MovementDate >= imqp.StartDate) &&
                            (detail.MovementDate <= imqp.EndDate)
                    orderby detail.MovementDate descending
                    select new ItemMovement(detail.SourceSystemId,
                                            (ItemMovementKind)detail.MovementKind,
                                            detail.MovementDate.DateTime,
                                            detail.UniversalMovementKey,
                                            detail.UniversalMaterialItemKey,
                                            ((detail.SourceDocumentRef != null) ? detail.SourceDocumentRef.Trim() : string.Empty),
                                            ((detail.SourceComment != null) ? detail.SourceComment.Trim().Replace("Sale: ", "").Substring(0, 20) : string.Empty),
                                            detail.ActualDeltaQty)
                        {
                            MovementKindName = detail.MovementKindName.Trim()
                        };
            register.AddRange(f.ToList<ItemMovement>());
        }

但不是这个:

public void FillRegister(ItemMovementRegister register, IDateRange imqp)
        {
            try
            {
                var f = from detail in this.Context.ShippingDocumentDetails
                        join header in this.Context.ShippingDocuments on detail.ClientOrderNumber equals header.ClientOrderNumber
                        where (header.DateOrdered >= imqp.StartDate)
                         && (header.DateOrdered <= imqp.EndDate)
                        orderby header.DateOrdered descending
                        select new ItemMovement(long.Parse(ConfigurationManager.AppSettings["PickedOrderSourceSystem"]),
                            ItemMovementKind.Picked,
                            ((header.DateOrdered.HasValue) ? header.DateOrdered.Value : new DateTime(1900, 1, 1)),
                             UniversalItemMovementConverter.GetMovementKeyFromShippingDocument(header.ClientOrderNumber),
                            detail.ProductCode,
                            header.ClientOrderNumber,
                            string.Empty,
                            ((detail.QuantityDelivered.HasValue) ? detail.QuantityDelivered.Value : 0)) { };
                List<ItemMovement> ms = f.ToList<ItemMovement>(); // Fails here
                this.UpdateItemMovements(ms);
                register.AddRange(ms);
            }
            catch (Exception ex)
            {
                throw new DALException("void FillItemMovements(ItemMovementRegister register, IDateRange imqp) failed :" + ex.Message, ex);
            }
        }  
4

1 回答 1

0

我对这样的结构有更好的运气:

var ms = (from detail in this.Context.ShippingDocumentDetails
                    join header in this.Context.ShippingDocuments on detail.ClientOrderNumber equals header.ClientOrderNumber
                    where (header.DateOrdered >= imqp.StartDate)
                     && (header.DateOrdered <= imqp.EndDate)
                    orderby header.DateOrdered descending
                    select new ItemMovement(long.Parse(ConfigurationManager.AppSettings["PickedOrderSourceSystem"]),
                        ItemMovementKind.Picked,
                        ((header.DateOrdered.HasValue) ? header.DateOrdered.Value : new DateTime(1900, 1, 1)),
                         UniversalItemMovementConverter.GetMovementKeyFromShippingDocument(header.ClientOrderNumber),
                        detail.ProductCode,
                        header.ClientOrderNumber,
                        string.Empty,
                        ((detail.QuantityDelivered.HasValue) ? detail.QuantityDelivered.Value : 0)) { }).ToList();
            this.UpdateItemMovements(ms);
            register.AddRange(ms);

我对这个主题的某些部分有点模糊,但我认为这与延迟执行有关。如果您将问题行更改为以下内容,您甚至可能会有更好的运气:

var ms = new List<ItemMovement>(f.ToList());

你甚至可能不需要f.ToList()f可能就够了。更精通这个主题的人可能能够更全面地解释,但我相信这与强制某种对象的实例化有关。

于 2012-04-20T03:42:56.443 回答