new Tuple(int.P-6ren">
gpt4 book ai didi

c# - 无法隐式转换类型 System.Collections.Generic.Dictionary, AnonymousType#1>

转载 作者:行者123 更新时间:2023-12-03 19:15:58 26 4
gpt4 key购买 nike

我在方法中有以下字典:

var nmDict = xelem.Descendants(plantNS + "Month").ToDictionary(
k => new Tuple<int, int, string>(int.Parse(k.Ancestors(plantNS + "Year").First().Attribute("Year").Value), Int32.Parse(k.Attribute("Month1").Value), k.Ancestors(plantNS + "Report").First().Attribute("Location").Value.ToString()),
v => {
var detail = v.Descendants(plantNS + "Details").First();
return
new
{
BaseHours = detail.Attribute("BaseHours").Value,
OvertimeHours = detail.Attribute("OvertimeHours").Value
};
});

我需要返回 nmDict。问题是我不知道如何标记我的方法签名。我尝试了以下方法:

protected IDictionary<XElement, XElement> OvertimereportData(HarvestTargetTimeRangeUTC ranges)

上面给了我这个错误:

Cannot implicitly convert type System.Collections.Generic.Dictionary<System.Tuple<int,int,string>,AnonymousType#1>' to 'System.Collections.Generic.IDictionary<System.Xml.Linq.XElement,System.Xml.Linq.XElement>'. An explicit conversion exists (are you missing a cast?) 


protected IDictionary<Tuple, XElement> OvertimereportData(HarvestTargetTimeRangeUTC ranges)

给我这个错误:

'System.Tuple': static types cannot be used as type arguments   

我不知道该怎么办

最佳答案

简短的回答:你不能从函数返回匿名类型。

长答案:你的字典的值类型是匿名的 {BaseHours, OvertimeHours} 不能从函数返回或作为参数传递(除了作为对象,但这对任何人都没有任何好处,除非你经历了反射(reflection)它的麻烦)。定义一个包含 BaseHoursOvertimeHours 的类/结构,或者使用元组。前者可能稍微好一些,因为您可以保留名称 BaseHoursOvertimeHours;使用元组,您只需得到 Value1Value2

关于c# - 无法隐式转换类型 System.Collections.Generic.Dictionary<System.Tuple<int,int,string>, AnonymousType#1>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11958393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com