LINQ查询中的对象初始值设定项-是否可以重用计算数据?

I'm using a linq query which looks (after some simplification) something like the following:

List<UserExams> listUserExams = GetUserExams();

var examData = 
from userExam in listUserExams
group by userExam.ExamID into groupExams
select new ExamData()
{
    ExamID = groupExams.Key,
    AverageGrade = groupExams.Average(e => e.Grade),
    PassedUsersNum = groupExams.Count(e => /* Some long and complicated calculation */),
    CompletionRate = 100 * groupExams.Count(e => /* The same long and complicated calculation */) / TotalUsersNum
};

What bothers me is the calculation expression which appears twice, for PassedUsersNum and CompletionRate.

Assuming that CompletionRate = (PassedUsersNum / TotalUsersNum) * 100, how can I write it by reusing the calculation of PassedUsersNum, instead of writing that expression again?

#0

The simplest way would be to use let to inject another selection step first:

List<UserExams> listUserExams = GetUserExams();

var examData = 
    from userExam in listUserExams
    group by userExam.ExamID into groupExams
    let passCount = groupExams.Count( /* long expression */)
    select new ExamData()
    {
        ExamID = groupExams.Key,
        AverageGrade = groupExams.Average(e => e.Grade),
        PassedUsersNum = passCount,
        CompletionRate = 100 * passCount / TotalUsersNum
    };

The expression will only be evaluated once per group, of course.

#1

You can also just extract your Count func into another method which returns a Func if you want, or a method that takes a double and returns a bool.

List<UserExams> listUserExams = GetUserExams();

var examData = 
from userExam in listUserExams
group by userExam.ExamID into groupExams
select new ExamData()
{
    ExamID = groupExams.Key,
    AverageGrade = groupExams.Average(funcMethod()),
    PassedUsersNum = groupExams.Count(e => traditionalMethod(e)),
    CompletionRate = 100 * groupExams.Count(e => /* The same long and complicated calculation */) / TotalUsersNum
};

// later...
private Func<double,bool> funcMethod(){ return e=> /* your calculation */ }

private bool traditionalMethod(double d){ return /* your calculation */ }

推荐文章

如何使用SimpleXML和PHP钻取SOAP名称空间

如何使用SimpleXML和PHP钻取SOAP名称空间

推荐文章

如何将日期时间传递给控制器操作

如何将日期时间传递给控制器操作

推荐文章

Facebook是否提供ajaxpush或类似的方法来避免持续的轮询?

Facebook是否提供ajaxpush或类似的方法来避免持续的轮询?

推荐文章

简单的套接字I/O问题…还是它?

简单的套接字I/O问题…还是它?

推荐文章

如何在OpenCv中从太空中探测太阳?

如何在OpenCv中从太空中探测太阳?

推荐文章

Magento-通过复制带有catalog前缀的所有数据库表来迁移产品

Magento-通过复制带有catalog前缀的所有数据库表来迁移产品

推荐文章

无法从内存中删除DLA/a服务

无法从内存中删除DLA/a服务

推荐文章

从结构中从txt读取信息的程序

从结构中从txt读取信息的程序

推荐文章

从对象内部的函数调用函数(对象文本)

从对象内部的函数调用函数(对象文本)

推荐文章

整数值在web服务调用中丢失

整数值在web服务调用中丢失

推荐文章

jsf2.0+Primefaces 2.x:将字符串绑定到日历

jsf2.0+Primefaces 2.x:将字符串绑定到日历

推荐文章

MVC3/Razor Add控制器“Get PrimaryKey”找不到主键

MVC3/Razor Add控制器“Get PrimaryKey”找不到主键

推荐文章

PHP语法错误?-创建一个多维数组,使用foreach循环输出HTML

PHP语法错误?-创建一个多维数组,使用foreach循环输出HTML

推荐文章

php除数集算法

php除数集算法

推荐文章

如何让实体框架和WCF处理事务?好吧…有什么秘密?

如何让实体框架和WCF处理事务?好吧…有什么秘密?

推荐文章

从viewcontroller从UIAlertView加载xib

从viewcontroller从UIAlertView加载xib