gpt4 book ai didi

c# - 查询中的多项选择

转载 作者:太空宇宙 更新时间:2023-11-03 22:45:04 27 4
gpt4 key购买 nike

此查询正在从 xml 文件构建商品符号。

文件结构为

<Commodities>
<Grains>
<Commodity title="Corn" value="0" name="corn">
<Specs>
//other elements
<SymbolRoot>
<Platform value="ZC" name="globex"/>
<Platform value="C " name="bloomberg"/>
</SymbolRoot>
</Specs>
<ContractMonths firstnoticedaterule="" lasttradedaterule="The business day prior to the 15th calendar day of the contract month">
<Month value="Mar">
<Year value="2018" firstnoticedate="02/28/18" lasttradedate="03/14/18" dateformat="mm/dd/yy"/>
<Year value="2019" firstnoticedate="02/28/19" lasttradedate="03/14/19" dateformat="mm/dd/yy"/>
<Year value="2020" firstnoticedate="02/28/20" lasttradedate="03/13/20" dateformat="mm/dd/yy"/>
</Month>
</ContractMonths>
</Commodity>
</Grains>
<Commodities>

目前,我可以根据需要获取合约月份,但我还需要传入平台的符号根。现在它被硬编码为“C”

private List<string> GetAllSymbolsForContractMonths(CommodityList commodity, string platform)
{
var symCode = string.Empty;
var yrLastDigit = DateTime.Now.Year % 10;
//get the contract month symbol codes
var query = _doc.Descendants("Commodity")
.Where(c => c.Attribute("name")?.Value == commodity.ToString().ToLower())
.Descendants("ContractMonths").Elements("Month")
.Select(v => "C " + SymbolHelpers.GetSymbolContractMonthLetter(v.Attribute("value")?.Value) + yrLastDigit + " Comdty")
.ToList();

return query;
}

我知道有一些方法可以对 Platform 元素的值属性进行选择并将该值设置为 symCode 变量,但我似乎无法正确选择。然后我可以用变量替换硬编码。

最佳答案

将您的查询一分为二。首先,找到商品元素。

从该元素中,找到平台符号。然后,构建列表。

private List<string> GetTypeFromVariable(CommodityList commodity, string platform)
{
var yrLastDigit = DateTime.Now.Year % 10;

var commodityElement = _doc.Descendants("Commodity")
.Where(x => x.Attribute("name")?.Value.Equals(commodity.ToString(), StringComparison.InvariantCultureIgnoreCase) ?? false)
.Single();

var symbol = commodityElement.Descendants("Platform")
.Where(x => x.Attribute("name")?.Value.Equals(platform, StringComparison.InvariantCultureIgnoreCase) ?? false)
.Single()
.Attribute("value").Value;

return commodityElement
.Descendants("ContractMonths").Elements("Month")
.Select(v => symbol + " " + SymbolHelpers.GetSymbolContractMonthLetter(v.Attribute("value")?.Value) + yrLastDigit + " Comdty")
.ToList();
}

关于c# - 查询中的多项选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50381059/

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