c# - 投影到复杂类型时无效的初始值设定项成员声明符

标签 c# linq object-initializers

我在下面的代码中使用简单的属性初始化对象,然后在其他地方重构,使 DispatchedDocumentDate 变成 DispatchedPhase.DocumentDate。我这样做是因为也有使用完全相同的属性出售和挑选的类(class)。

所以现在在引用程序集中我有这段代码无法编译:

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

我得到一个:

Invalid initializer member declarator

错误信息。

希望意图很明确,但我不确定如何重写。我用 Google 搜索了一下,得到了一些关于 Let 的信息,但仍然不清楚。

最佳答案

在这一点上,我将继续向 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>();
    }

关于c# - 投影到复杂类型时无效的初始值设定项成员声明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9764276/

相关文章:

c# - 单例运行在 Asp.Net web 应用程序上

c# - 如何跳过 `foreach` 循环的迭代?

c# - 刷新 UI 以反射(reflect)添加到列表中的项目

c# - Linq:合并字典

c# - 在所有者对象初始化器中初始化 get-only 属性

c# - 是否可以减小来自字节数组的图像的大小

entity-framework - Entity Framework : Left Join with List Result

c# - 如何确定 C# 中字符串列表中的重复项?

以临时对象为参数的 C++ 对象构造函数

c# - 是否可以在 bool 上使用对象初始化器?