投影到复杂类型时,初始化程序成员声明符无效

时间:2012-03-19 02:31:26

标签: c# linq object-initializers

我正在使用简单属性初始化下面代码中的对象,但随后在其他地方重构,以便DispatchedDocumentDate成为DispatchedPhase.DocumentDate。我这样做是因为还有使用完全相同的属性销售和挑选的类。

所以现在在引用程序集中我有这个不编译的代码:

 public List<ItemMovementEntry> FillItemDispatchMovements(IEntityDateRange imqp)
        {
            var f = from detail in this.Context.DispatchDetails
                    join header in this.Context.Dispatches on detail.ClientOrderNumber equals header.ClientOrderNumber
                    where (detail.ProductCode == imqp.ItemKey)
                     && (header.DateOrdered >= imqp.StartDate)
                     && (header.DateOrdered <= imqp.EndDate)
                    orderby header.DateOrdered descending
                    select new ItemMovementEntry(ItemMovementEntryKind.Dispatch)
                    {
                        DispatchedPhase.DocumentDate = ((header.DateOrdered.HasValue) ? header.DateOrdered.Value : new DateTime(1900, 1, 1)),
                        DispatchedPhase.DocumentLKey = header.ClientOrderNumber,
                        MaterialItemLkey = detail.ProductCode,
                        DispatchedPhase.MovementDeltaQty = ((detail.QuantityDelivered.HasValue) ? (-1) * detail.QuantityDelivered.Value : 0),
                        DispatchedPhase.Comment = string.Empty,
                        JournalType = "DISPATCHED",
                    };
            return f.ToList<ItemMovementEntry>();
        }

我得到了:

  

无效的初始化成员声明符

错误消息。

希望意图很明确,但我不确定如何重写。我用Google搜索并得到了一些关于Let的内容,但目前还不清楚。

2 个答案:

答案 0 :(得分:1)

此时我将专门为ItemMovementEntry类添加一个额外的构造函数来处理这个问题。

   public ItemMovementEntry(ItemMovementEntryKind comparerMovementKind,
                    DateTime documentDate,
                    string documentLKey,
                    string materialItemKey,
                    int movementDeltaQty,
                    string comment)
            : this(comparerMovementKind)
        {
            ItemMovementEntryPhase p = null;
            switch (comparerMovementKind)
            {
                case ItemMovementEntryKind.Sales:
                    p = this.SoldPhase;
                    break;
                case ItemMovementEntryKind.Picking:
                    p = this.PickedPhase;
                    break;
                case ItemMovementEntryKind.Dispatch:
                    p = this.DispatchedPhase;
                    this.JournalType = "DISPATCHED";
                    break;
            }
            p.DocumentDate = documentDate;
            p.DocumentLKey = documentLKey;
            this.MaterialItemLkey = materialItemKey;
            p.MovementDeltaQty = movementDeltaQty;
            p.Comment = comment;
        }

    public List<ItemMovementEntry> FillItemDispatchMovements(IEntityDateRange imqp)
    {
        var f = from detail in this.Context.DispatchDetails
                join header in this.Context.Dispatches on detail.ClientOrderNumber equals header.ClientOrderNumber
                where (detail.ProductCode == imqp.ItemKey)
                 && (header.DateOrdered >= imqp.StartDate)
                 && (header.DateOrdered <= imqp.EndDate)
                orderby header.DateOrdered descending
                select new ItemMovementEntry(ItemMovementEntryKind.Dispatch,
                    ((header.DateOrdered.HasValue) ? header.DateOrdered.Value : new DateTime(1900, 1, 1)),
                    header.ClientOrderNumber,
                    detail.ProductCode,
                    ((detail.QuantityDelivered.HasValue) ? (-1) * detail.QuantityDelivered.Value : 0),
                    string.Empty){};
        return f.ToList<ItemMovementEntry>();
    }

答案 1 :(得分:0)

您可以使用别名设置的直接类型构建属性吗?例如如果您持有复杂类型,请创建一个属性,该属性具有您要设置的子实体属性的setter。