我已經使用了這個錯誤,並在堆棧溢出中閱讀了一些帖子,但我仍然不明白這裏出現了什麼問題。我理解「英語」,但不懂程序化推理。爲什麼我能做到這一點:Linq:不允許在查詢中顯式構造實體類型
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);
}
}
我從來沒有理解過它,但是你不能在你的'select'中使用LINQ類型,除非它是被選擇的實際類型。你將不得不解決一個爲返回值而定製的自定義類結構,或者一個匿名類。 – mellamokb 2012-04-20 03:29:43
如果你把這個作爲答案,我會接受。這是我見過的最清晰的解釋。簡單的兩句話。從那我可以前進。如果這樣的規則很好,我從來沒有看到它明確表示任何地方。 Thnx – rism 2012-04-20 05:02:08
[該實體不能在LINQ to Entities查詢中構建]的可能重複(http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities - 查詢) – 2012-04-21 19:48:39