首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用已计算的元素

使用已计算的元素
EN

Stack Overflow用户
提问于 2014-05-07 08:40:13
回答 2查看 88关注 0票数 5

我发现自己在linq语句中重复计算,我想知道是否可以以某种方式访问已经计算过的元素。这就是我要做的:

代码语言:javascript
复制
var result = testdata.GroupBy(a => new { a.reportinggroup, a.commodity, a.timestep.Year })
                     .Select(g => new EndResult
                            {
                                rundate = rundate.ToShortDateString(),
                                price = g.Sum(a => (a.timestep.ispeak()) ? a.price : 0) / g.Sum(a => (a.timestep.ispeak()) ? 1 : 0),
                                valueposition = g.Sum(a => (a.timestep.ispeak()) ? a.position * a.price : 0) / (g.Sum(a => (a.timestep.ispeak()) ? a.price : 0) / g.Sum(a => (a.timestep.ispeak()) ? 1 : 0)),

                            }).ToList();

效果很好。

这就是我想要做的:

代码语言:javascript
复制
var result = testdata.GroupBy(a => new { a.reportinggroup, a.commodity, a.timestep.Year })
                     .Select(g => new EndResult
                            {
                                rundate = rundate.ToShortDateString(),
                                price = g.Sum(a => (a.timestep.ispeak()) ? a.price : 0) / g.Sum(a => (a.timestep.ispeak()) ? 1 : 0),
                                valueposition = g.Sum(a => (a.timestep.ispeak()) ? a.position * a.price : 0) / price,           
                            }).ToList();

其中价格是我在运行日期之后计算出来的价格。我能以某种方式访问这个吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-07 08:43:17

您可以首先选择一个匿名类型来存储此值:

代码语言:javascript
复制
result = testdata.GroupBy(a => new { a.reportinggroup, a.commodity, a.timestep.Year })
.Select(g => new { 
    rundate = rundate.ToShortDateString(), 
    price = g.Sum(a => (a.timestep.ispeak()) ? a.price : 0) / g.Sum(a => (a.timestep.ispeak()) ? 1 : 0),
    group = g
})
.Select(x => new EndResult
{
    rundate = x.rundate,
    price = x.price,
    valueposition = x.group.Sum(a => (a.timestep.ispeak()) ? a.position * x.price : 0) / x.price
}).ToList();

另一种方法是使用“真实”代码:

代码语言:javascript
复制
result = testdata.GroupBy(a => new { a.reportinggroup, a.commodity, a.timestep.Year })
.Select(g => { 
    var price = g.Sum(a => (a.timestep.ispeak()) ? a.price : 0) / g.Sum(a => (a.timestep.ispeak()) ? 1 : 0);
    EndResult endRes = new EndResult { 
        rundate = rundate.ToShortDateString(),
        price = price,
        valueposition = x.group.Sum(a => (a.timestep.ispeak()) ? a.position * price : 0) / price)
    };
    return endRes;
}).ToList();
票数 2
EN

Stack Overflow用户

发布于 2014-05-07 08:48:19

通过使用查询语法 (from),您可以使用子句:

代码语言:javascript
复制
 var result = (from g in  testdata.GroupBy(a => new { a.reportinggroup, a.commodity, a.timestep.Year })
               let price = g.Sum(a => (a.timestep.ispeak()) ? a.price : 0) / g.Sum(a => (a.timestep.ispeak()) ? 1 : 0)
               select new EndResult
               {
                   rundate = rundate.ToShortDateString(),
                   price = price,
                   valueposition = g.Sum(a => (a.timestep.ispeak()) ? a.position * a.price : 0) / price,
                }).ToList();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23512856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档