gpt4 book ai didi

c# - Linq 选择查询,如何将结果附加到类的对象

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:14 24 4
gpt4 key购买 nike

在下面截取的 c# 代码中,我需要添加到这个 IEnumerable 集合,而不是每次都创建一个新的 tradeContributions

我以为我可以执行 tradeContributions.add()add() 方法不可用。

public static IEnumerable<TradeContribution> GetTradeContributions(uint portfolioId, List<uint> portfolioIdList, IEnumerable<DateTime> nodeDateList, int? whatIfBatchNumber = null)
{

// THIS IS THE ORIGINAL CODE
IEnumerable<TradeContribution> tradeContributions = new List<TradeContribution> { };


tradeContributions = (from tc in xml.XPathSelectElement("body/portfolios/portfolio/contributions").Elements("tradeContribution")
select new TradeContribution
{
SysId = tc.Attribute("sysId") == null ? 0 : int.Parse(tc.Attribute("sysId").Value),
TradeContextId = tc.Attribute("contextId") == null ? 0 : int.Parse(tc.Attribute("contextId").Value),
TradeId = tc.Attribute("tradeId") == null ? "" : tc.Attribute("tradeId").Value,
ProductType = tc.Attribute("desc").Value,
ProductDescription = tc.Attribute("desc").Value, // TODO: In future could lookup the description in a reference data cache
Contribution = decimal.Parse(tc.Attribute("contribution").Value)
})
.OrderByDescending(x => x.Contribution);



// ... code omitted for brevity

// THIS IS MY NEW CODE TO HANDLE THE NEW REQUIREMENTS
foreach (XElement pfElem in xml.XPathSelectElements("body/portfolios/portfolio"))
{
tradeContributions = (from tc in pfElem.XPathSelectElement("contributions").Elements("tradeContribution")
select new TradeContribution
{
SysId = tc.Attribute("sysId") == null ? 0 : int.Parse(tc.Attribute("sysId").Value),
TradeContextId = tc.Attribute("contextId") == null ? 0 : int.Parse(tc.Attribute("contextId").Value),
TradeId = tc.Attribute("tradeId") == null ? "" : tc.Attribute("tradeId").Value,
ProductType = tc.Attribute("desc").Value,
ProductDescription = tc.Attribute("desc").Value,
Contribution = decimal.Parse(tc.Attribute("contribution").Value)
}
);
}

return tradeContributions;
}
}

如何将每个新的 tradeContribution 添加到我的收藏中?

最佳答案

我对这里的这一行有疑问:

IEnumerable<TradeContribution> tradeContributions = new List<TradeContribution> { };

这是一个局部变量...所以我们为什么要将它锁定到一个有限的合约中,作为一个 IEnumerable ?真的是List<T> ,所以只需像这样声明它:

var  tradeContributions = new List<TradeContribution> { };

完成后,您需要做的就是更改 foreach 中的代码块。到:

tradeContributions.AddRange((from tc in pfElem.XPathSelectElement("contributions").Elements("tradeContribution")
select new TradeContribution
{
SysId = tc.Attribute("sysId") == null ? 0 : int.Parse(tc.Attribute("sysId").Value),
TradeContextId = tc.Attribute("contextId") == null ? 0 : int.Parse(tc.Attribute("contextId").Value),
TradeId = tc.Attribute("tradeId") == null ? "" : tc.Attribute("tradeId").Value,
ProductType = tc.Attribute("desc").Value,
ProductDescription = tc.Attribute("desc").Value,
Contribution = decimal.Parse(tc.Attribute("contribution").Value)
}));

基本上,恕我直言,通过使用 IEnumerable ,你正在创造一个人为的限制,如果跨越了一些逻辑边界,这个限制可能是有意义的。但是没有……所以你不应该。

更新:

好的,现在我看到了整个方法的代码,我可以理解(某种程度上)为什么 IEnumerable作出声明。我有点认为变量是多余的。你只需要 Concat()两个 LINQ 一起返回结果,恕我直言。

有点像这样:

public static IEnumerable<TradeContribution> GetTradeContributions(uint portfolioId, List<uint> portfolioIdList, IEnumerable<DateTime> nodeDateList, int? whatIfBatchNumber = null)
{
var originalItems = (from tc in xml.XPathSelectElement("body/portfolios/portfolio/contributions").Elements("tradeContribution")
select new TradeContribution
{
SysId = tc.Attribute("sysId") == null ? 0 : int.Parse(tc.Attribute("sysId").Value),
TradeContextId = tc.Attribute("contextId") == null ? 0 : int.Parse(tc.Attribute("contextId").Value),
TradeId = tc.Attribute("tradeId") == null ? "" : tc.Attribute("tradeId").Value,
ProductType = tc.Attribute("desc").Value,
ProductDescription = tc.Attribute("desc").Value, // TODO: In future could lookup the description in a reference data cache
Contribution = decimal.Parse(tc.Attribute("contribution").Value)
})
.OrderByDescending(x => x.Contribution);



// ... code omitted for brevity

// THIS IS MY NEW CODE TO HANDLE THE NEW REQUIREMENTS
var additionalItems = xml.XPathSelectElements("body/portfolios/portfolio")
.SelectMany(pfElem =>
{
(from tc in pfElem.XPathSelectElement("contributions").Elements("tradeContribution")
select new TradeContribution
{
SysId = tc.Attribute("sysId") == null ? 0 : int.Parse(tc.Attribute("sysId").Value),
TradeContextId = tc.Attribute("contextId") == null ? 0 : int.Parse(tc.Attribute("contextId").Value),
TradeId = tc.Attribute("tradeId") == null ? "" : tc.Attribute("tradeId").Value,
ProductType = tc.Attribute("desc").Value,
ProductDescription = tc.Attribute("desc").Value,
Contribution = decimal.Parse(tc.Attribute("contribution").Value)
}
});

return originalItems.Concat(additionalItems);
}

关于c# - Linq 选择查询,如何将结果附加到类的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35544259/

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