Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# of)为什么作出IEnumerable声明。我认为这个变量是多余的。您只需将两个LINQ一起Concat()并返回结果IMHO_C#_Linq_Linq To Sql - Fatal编程技术网

C# of)为什么作出IEnumerable声明。我认为这个变量是多余的。您只需将两个LINQ一起Concat()并返回结果IMHO

C# of)为什么作出IEnumerable声明。我认为这个变量是多余的。您只需将两个LINQ一起Concat()并返回结果IMHO,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,有点像这样: public static IEnumerable<TradeContribution> GetTradeContributions(uint portfolioId, List<uint> portfolioIdList, IEnumerable<DateTime> nodeDateList, int? whatIfBatchNumber = null) { var originalItems = (from tc in xml.XPa

有点像这样:

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);
}
public static IEnumerable GetTradeContributions(uint portfolioId,List portfolioIdList,IEnumerable nodeDateList,int?whatIfBatchNumber=null)
{
var originalItems=(来自xml.XPathSelectElement(“主体/组合/组合/贡献”)中的tc.Elements(“交易贡献”)
选择新的贸易贡献
{
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.属性(“描述”)值,
ProductDescription=tc.Attribute(“desc”).Value,//TODO:将来可以在引用数据缓存中查找描述
贡献=decimal.Parse(tc.Attribute(“贡献”).Value)
})
.OrderByDescending(x=>x.Contribution);
//…为简洁起见,省略了代码
//这是我处理新需求的新代码
var additionalItems=xml.XPathSelectElements(“主体/组合/组合”)
.SelectMany(pfElem=>
{
(来自pfElem.XPathSelectElement(“贡献”).Elements(“交易贡献”)中的tc)
选择新的贸易贡献
{
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.属性(“描述”)值,
ProductDescription=tc.属性(“描述”)值,
贡献=decimal.Parse(tc.Attribute(“贡献”).Value)
}
});
返回原始目录(附加项);
}

tradeContributions为什么是
IEnumerable
?tradeContributions为什么是
IEnumerable
?是的,我也不明白以前的开发人员为什么创建
tradeContributions
作为IEnumerable。所以我把我的原始帖子更新到显示在添加新的
foreach
节之前我正在处理的代码块。此外,如果我尝试将原始tradeContributions对象更改为
new List{}
,那么顶部的原始
选择新
部分将导致某种异常。这让我有点困惑,所以我将为您给我的
列表
想法使用一个新的变量。谢谢。对,我也不明白为什么以前的开发人员将
tradeContributions
创建为IEnumerable。所以我更新了我的原始版本post显示我在添加新的
foreach
节之前处理的代码块。此外,如果我尝试将原始tradeContributions对象更改为
new List{}
,则顶部原始的
选择新的
部分将导致某种异常。这对我来说有点混乱,因此我将为您给我的
列表
想法使用一个新的变量。谢谢。
List<TradeContribution> tradeContributions = new List<TradeContribution> { };
foreach (XElement pfElem in xml.XPathSelectElements("body/portfolios/portfolio"))
{
    var temp = (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)
      }
    );
    tradeContributions.AddRange(temp.ToArray()); //then add the results to the List
}
foreach (XElement pfElem in xml.XPathSelectElements("body/portfolios/portfolio"))
{
    var temp = (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)
      }
    );
    tradeContributions = tradeContributions.Concat(temp);
}
IEnumerable<TradeContribution> tradeContributions = new List<TradeContribution> { };
var  tradeContributions = new List<TradeContribution> { };
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)
  }));
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);
}