在LINQ中的select语句中执行计算 [英] Perform calculation inside select statement in LINQ

查看:66
本文介绍了在LINQ中的select语句中执行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,我必须计算两个值的百分比 例如

I have a situation where i have to calculate percentage of two values for example

IEnumerable<RenewalModel> result = 
    from r in renewalLists
    group r by r.CityID into grpCity
    select new RenewalModel
    {
        CityID = grpCity.Key,
        City = (from g in grpCity where g.CityID == grpCity.Key select g.City).First().Trim(),
        PotentialRenewalCount = (from g in grpCity where g.CityID == grpCity.Key select g.PotentialRenewalCount).Sum(),
        PotentialRenewalSQRT = (from g in grpCity where g.CityID == grpCity.Key select g.PotentialRenewalSQRT).Sum(),
        desiredCalucation=   (PotentialRenewalCount/PotentialRenewalCount)*100,
        RENEWALCOUNT = (from g in grpCity where g.CityID == grpCity.Key select g.RENEWALCOUNT).Sum(),
        RENEWALSQRT = (from g in grpCity where g.CityID == grpCity.Key select g.RENEWALSQRT).Sum()
    };

我的计算应该是这样

(PotentialRenewalCount/PotentialRenewalCount)* 100

(PotentialRenewalCount/PotentialRenewalCount)*100

正如我在select语句中描述的那样.

as i have described it inside the select statement.

我什至尝试了该查询,但结果为0

i even tried this query but i get the 0 as result

 IEnumerable<RenewalModel> result =
            (from r in renewalLists
            group r by r.CityID into grpCity
            select new RenewalModel
            {
                CityID = grpCity.Key,
                City = (from g in grpCity where g.CityID == grpCity.Key select g.City).First().Trim(),
                PotentialRenewalCount = (from g in grpCity where g.CityID == grpCity.Key select g.PotentialRenewalCount).Sum(),
                PotentialRenewalSQRT = (from g in grpCity where g.CityID == grpCity.Key select g.PotentialRenewalSQRT).Sum(),
                RENEWALCOUNT = (from g in grpCity where g.CityID == grpCity.Key select g.RENEWALCOUNT).Sum(),
                RENEWALSQRT = (from g in grpCity where g.CityID == grpCity.Key select g.RENEWALSQRT).Sum()
            }).select(r => new RenewalModel
            {
                desiredCalucation = (r.PotentialRenewalCount / r.PotentialRenewalCount) * 100,
                CityID = r.CityID,
                City = r.City,
                PotentialRenewalCount = r.PotentialRenewalCount,
                PotentialRenewalSQRT = r.PotentialRenewalSQRT,
                RENEWALCOUNT = r.RENEWALCOUNT,
                RENEWALSQRT = r.RENEWALSQRT
            });

由于某种原因或另一个desiredCalucation变量给我0作为结果.

for some reason or another desiredCalucation variable is giving me 0 as result.

任何帮助,我们感激不尽. 谢谢

any help is appreciated. Thank you

推荐答案

为此,请使用LINQ 让运算符 > .

For this purpose, use the LINQ let operator.

通常,我建议将大型LINQ语句拆分为几个较小的语句.否则,稍后进行调试将非常痛苦.

In general, I would recommend to split your big LINQ statement on several smaller ones. Otherwise, it will be very painful to debug this later.

这篇关于在LINQ中的select语句中执行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆