【问题标题】:Converting anonymous type to List<KeyValuePair>将匿名类型转换为 List<KeyValuePair>
【发布时间】:2012-01-23 07:00:43
【问题描述】:

我有一个方法需要以下内容:

public static List<ParetoElement> 
     ParetoBuildBySum(List<KeyValuePair<string, double>> inputData)

我有以下 linq 查询,并希望传入 KeyValuePairs(字符串和双精度)列表中的这两个匿名值。

var myHistoSource = from d in data 
                    select new 
                    { 
                       Type = d.Item_Expense_Type, 
                       Amount = Double.Parse(d.Item_Amount.ToString()) 
                    };

正确的方法是什么?

谢谢

【问题讨论】:

    标签: c# linq type-conversion key-value


    【解决方案1】:

    只需相应地修改您的查询:

    var myHistoSource = (
        from d in data 
        select new KeyValuePair<string, double>(d.Item_Expense_Type,
                                                Double.Parse(d.Item_Amount.ToString())
        ).ToList();
    

    顺便说一句,您的 ParetoBuildBySum 方法应该很可能接受 IEnumerable 而不是 List 作为其参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 2012-05-01
      • 2020-04-18
      相关资源
      最近更新 更多