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

标签 c# linq linq-to-sql

在下面截取的 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/

相关文章:

c# - 替代使用 if -else 语句

c# - 为什么以下代码在 WCF 服务与控制台应用程序中运行速度相当慢?

c# - 如何限制长时间运行的命令行 EXE 以避免占用 CPU?

c# - 检查 List<object> 是否包含所有整数

linq-to-sql - LinqToSql 最佳实践

c# - 每次创建一张新表还是使用一张表更好?

c# - 如何向 ThenInclude 添加 where 子句

c# - 寻找最大问题

sql-server - 如何使用 LINQ-to-SQL 查询 sys 表?

c# - Asp.Net MVC - Rob Conery 的 LazyList - Count() 或 Count