无效参数——不能转换匿名类型#2

本文关键字:类型 转换 参数 不能 无效 | 更新日期: 2023-09-27 18:18:21

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

我得到一个错误

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

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

如果我在Linqpad中尝试它,它告诉我"名称' ext '在当前上下文中不存在"

任何想法?谢谢!


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

无效参数——不能转换匿名类型#2

问题是你的收藏。

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的集合

现在你的代码初始化另一个匿名类型的对象summary,因为没有办法识别它是否与第一个查询中创建的相同,所以它将summary创建为匿名类型#2。现在将其添加到导致错误的第一个类型的集合l中。

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

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

select new yourclass {//所有属性}

//创建集合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();

这两种匿名类型并不相同。属性可能具有相同的名称,但可能不具有相同的类型,它们大多数具有相同的名称和类型。

你应该考虑创建自己的类,并使用它。