gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:53 25 4
gpt4 key购买 nike

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

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

最佳答案

我对这样的构造比较幸运:

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 可能就足够了。更精通该主题的人可能能够更全面地解释,但我相信这与强制对象的某种实例化有关。

关于c# - Linq:不允许在查询中显式构造实体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10239617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com