none
Overcoming multiple anonymous type error RRS feed

  • Question

  • Hi,

    I'm a bit of a newbie and struggling to understand the following error:

    "The best overloaded method match for 'System.Collections.Generic.List.Add(AnonymousType#1)' has some invalid arguments Argument '1': cannot convert from 'AnonymousType#2' to 'AnonymousType#1'"

    It's occurring on the following script. 

    Any suggestions gratefully received!!

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

    Monday, December 19, 2011 11:31 PM

Answers

  • Although to you AnonymousType#1  and AnonymousType#2 are the same type, they are not the same for the compiler so when you try to add summary to your list this happens.  In this case you can try to make a container class.

    Regards

    Tuesday, December 20, 2011 2:50 PM