简体   繁体   English

字典无法从 ' 转换<anonymous type> '</anonymous>

[英]Dictionary cannot convert from '<anonymous type>'

Here's my code:这是我的代码:

var groupedDataDictionary = products
    .Where(p => p.ProductType == ProductType.ValueType)
    .GroupBy(p => (p.OfficeDebitId, p.OfficeDebitDate, p.PaymentMethod))
    .ToDictionary(x => x.Key, x => x.Sum(p => p.Amount)); 

var result = products.Select(p => new ResponseDto()
    {
        customer_id = p.CustomerId, 
        office_debit_date = p.OfficeDebitDate.Value.ToString(),
        office_debit_id = p.OfficeDebitId.ToString(),
        office_debit_total = groupedDataDictionary[new { p.OfficeDebitId, p.OfficeDebitDate, p.PaymentMethod }].Sum().ToString(), // this line causes error
        payment_method = p.PaymentMethod.Value.ToString(),
    }).ToList();

When assiging office_debit_total on that line it says:在该行上分配office_debit_total时,它说:

Error CS1503 Argument 1: cannot convert from '<anonymous type: string OfficeDebiId, System.DateTime? OfficeDebitDate, Enumerations.PaymentMethod? PaymentMethod>'错误 CS1503 参数 1:无法从'<anonymous type: string OfficeDebiId, System.DateTime? OfficeDebitDate, Enumerations.PaymentMethod? PaymentMethod>' '<anonymous type: string OfficeDebiId, System.DateTime? OfficeDebitDate, Enumerations.PaymentMethod? PaymentMethod>' '<anonymous type: string OfficeDebiId, System.DateTime? OfficeDebitDate, Enumerations.PaymentMethod? PaymentMethod>' to '(string OfficeDebitId, System.DateTime? OfficeDebitDate, Enumerations.PaymentMethod? PaymentMethod)' '<anonymous type: string OfficeDebiId, System.DateTime? OfficeDebitDate, Enumerations.PaymentMethod? PaymentMethod>' to '(string OfficeDebitId, System.DateTime? OfficeDebitDate, Enumerations.PaymentMethod? PaymentMethod)'

You should use either Tuples or Anonymous Types , but do not combine them.您应该使用TuplesAnonymous Types ,但不要组合它们。 If you want to use Tuples for keys then you should also use Tuples to get dictionary values by keys:如果您想使用Tuples作为键,那么您还应该使用Tuples通过键获取字典值:

var result = products.Select(p => new ResponseDto()
    {
        customer_id = p.CustomerId, 
        office_debit_date = p.OfficeDebitDate.Value.ToString(),
        office_debit_id = p.OfficeDebitId.ToString(),

        // Here you should use Tuple to get value by Key.
        office_debit_total = groupedDataDictionary[(p.OfficeDebitId, p.OfficeDebitDate, p.PaymentMethod)].ToString(),

        payment_method = p.PaymentMethod.Value.ToString(),
    }).ToList();

Also note that I deleted invocation of the method Sum() , because dictionary groupedDataDictionary already contains aggregated data: x.Sum(p => p.Amount) .另请注意,我删除了方法Sum()的调用,因为字典groupedDataDictionary已经包含聚合数据: x.Sum(p => p.Amount)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将匿名类型#1 []隐式转换为匿名类型#2 [] - Cannot implicitly convert anonymous type #1[] to anonymous type #2[] 无法隐式转换类型<anonymous type></anonymous> - Cannot implicitly convert type <anonymous type> 无效的参数-无法转换匿名类型2 - Invalid Arguments - cannot convert Anonymous Type#2 asp.net C#:无法从'string'转换为'<anonymous type error>' - asp.net C# :cannot convert from 'string' to '<anonymous type error>' 无法从“System.Guid”转换为“ <anonymous type: System.Linq.IQueryable<System.Guid> 模板审核ID&gt; - Cannot convert from 'System.Guid' to '<anonymous type: System.Linq.IQueryable<System.Guid> TemplateReviewId> EF Core 无法从匿名类型的查询 select 转换为 DTO object class - EF Core cannot convert from query select of anonymous type to DTO object class 无法将类型&#39;Anonymous Type#1 []&#39;隐式转换为&#39;int []&#39; - Cannot implicitly convert type 'Anonymous Type#1[]' to 'int[]' 不能隐式转换类型 List&lt;<anonymous type: ..> &gt; 到 IEnumerable<Product> - Cannot implicitly convert type List<<anonymous type: ..>> to IEnumerable<Product> 无法从字典转换为IDictionary - cannot convert from Dictionary to IDictionary 无法将 Jobject 类型转换为字典<string, entityproperty></string,> - Cannot convert Jobject type to a Dictionary<string, EntityProperty>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM