Linq:不允许在查询中显式构造实体类型

时间:2012-04-20 02:53:28

标签: c# linq

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

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

1 个答案:

答案 0 :(得分: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即可。更熟悉这个主题的人可能能够更全面地解释,但我认为它与强迫对象的某种实例化有关。