【问题标题】:Invalid Arguments - cannot convert Anonymous Type#2无效参数 - 无法转换匿名类型#2
【发布时间】:2011-12-20 23:29:05
【问题描述】:

我正在尝试对列表进行小计,其中某些列需要求和或平均,而其他列与小计无关,例如股票名称。

我遇到了一个错误

"最佳重载方法匹配 'System.Collections.Generic.List.Add(AnonymousType#1)' 有一些无效的参数 参数 '1': 不能从 'AnonymousType#2' 到 'AnonymousType#1'"

在 Sage 200 查询生成器中运行它时,看不到我做错了什么。

如果我在 Linqpad 中尝试它,它会告诉我“名称 'cxt' 在当前上下文中不存在”

有什么想法吗?谢谢!


var q = from d in cxt.K3_StockProfitMarginByCustomers
select new
{
    d.CustomerAccountName,
    d.CustomerAccountNumber,
    d.Code,
    d.Name,
    d.Profit,
    d.QuantitySold,
    d.TotalCost,
    d.TotalRevenue,
    d.MARGIN,
    d.SLCustomerAccountID,
    d.SOPOrderReturnID,
    d.SOPOrderReturnLineID
};

q = q.Distinct();

var l = q.ToList();
var summary = new 
{ 
   CustomerAccountName = "",
   CustomerAccountNumber = "",
   Code = "",
   Name = "", 
   Profit = (Decimal)q.Sum(o => o.Profit),
   QuantitySold = (Decimal)q.Sum(o => o.QuantitySold),
   TotalCost= (Decimal)q.Sum(o => o.TotalCost),
   TotalRevenue= (Decimal)q.Sum(o => o.TotalRevenue),
   MARGIN = (Decimal)q.Average(o => o.MARGIN),
   SLCustomerAccountID=(String)"",
   SOPOrderReturnID=(String)"",
   SOPOrderReturnLineID=(String)""
};

l.Add(summary);

return l.AsQueryable();

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    您的收藏有问题。

    var q = from d in cxt.K3_StockProfitMarginByCustomers
    
    select new
    {
    d.CustomerAccountName,
    d.CustomerAccountNumber,
    d.Code,
    d.Name,
    d.Profit,
    d.QuantitySold,
    d.TotalCost,
    d.TotalRevenue,
    d.MARGIN,
    d.SLCustomerAccountID,
    d.SOPOrderReturnID,
    d.SOPOrderReturnLineID
    };
    

    这将创建匿名类型对象作为可查询的,当你这样做时

    q = q.Distinct();
    var l = q.ToList();
    

    它创建匿名类型#1 的集合

    现在您的代码初始化另一个匿名类型的对象摘要,因为无法识别它与第一个查询中创建的相同,因此它将摘要创建为匿名类型#2。现在您将其添加到导致错误的第一个 typeof 集合 l 中。

    解决方案: 创建包含所有属性的强类型类并使用它

    第一个查询和摘要的字母。

    选择新的你的班级{ // 所有属性 }

    // 创建集合 var summary = new yourclass(){ // 赋值 } // 添加摘要到集合。

    这将解决您的问题。

    这是完整的例子

    // Create new CS file with Name MyClass
    public class MyClass
    {
      public string CustomerAccountName {get; set;},
        public string CustomerAccountNumber {get; set;},
          public string Code {get; set;},
          public string Name {get; set;},
          public string Profit {get; set;},
          public int QuantitySold {get; set;},
        public double  TotalCost {get; set;},
        public double TotalRevenue {get; set;},
        public double MARGIN {get; set;},
        public int SLCustomerAccountID {get; set;},
        public int SOPOrderReturnID {get; set;},
        public int SOPOrderReturnLineID {get; set;}
    }
    
    
    //
    var q = from d in cxt.K3_StockProfitMarginByCustomers
    select new MyClass()
    {
       CustomerAccountName= d.CustomerAccountName,
       CustomerAccountNumber = d.CustomerAccountNumber,
       Code = d.Code,
       Name = d.Name,
       Profile = d.Profit,
       QuantitySold= d.QuantitySold,
       TotalCost = d.TotalCost,
       TotalRevenue= d.TotalRevenue,
       MARGIN = d.MARGIN,
       SLCustomerAccountID= d.SLCustomerAccountID,
       SOPOrderReturnID= d.SOPOrderReturnID,
       SOPOrderReturnLineID= d.SOPOrderReturnLineID
    };
    
    q = q.Distinct();
    
    var l = q.ToList();
    var summary = new MyClass()
    { 
       CustomerAccountName = "",
       CustomerAccountNumber = "",
       Code = "",
       Name = "", 
       Profit = (Decimal)q.Sum(o => o.Profit),
       QuantitySold = (Decimal)q.Sum(o => o.QuantitySold),
       TotalCost= (Decimal)q.Sum(o => o.TotalCost),
       TotalRevenue= (Decimal)q.Sum(o => o.TotalRevenue),
       MARGIN = (Decimal)q.Average(o => o.MARGIN),
       SLCustomerAccountID=(String)"",
       SOPOrderReturnID=(String)"",
       SOPOrderReturnLineID=(String)""
    };
    
    l.Add(summary);
    
    return l.AsQueryable();
    

    【讨论】:

    • 您好,非常感谢您的回复。我有点新手,仍在努力理解解决方案。我花了几个小时试图理解但没有得到任何结果:(你是否有可能通过修改包含你的观点的查询来向我展示?
    • 1.在 C# 中创建包含您在查询中选择的所有特性的类。 (MyClass 是类名) 2. from d in cxt.K3_StockProfitMarginByCustomers select new MyClass() { CustomerAccountName = d.CustomerAccountName, d.CustomerAccountNumber, // 对所有属性执行上述操作 d.Code, d.Name, d.Profit, d .QuantitySold, d.TotalCost, d.TotalRevenue, d.MARGIN, d.SLCustomerAccountID, d.SOPOrderReturnID, d.SOPOrderReturnLineID };
    【解决方案2】:

    这两种匿名类型不一样。属性可能具有相同的名称,但可能不同的类型,它们大多数都被认为是相同的类型。

    您应该考虑创建自己的类并改用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      相关资源
      最近更新 更多