C# LINQ 查询
我在用户定义类型列表中有一些数据,其中包含以下数据:
名称、研究、组、结果、日期。现在我想获取姓名、研究和小组,然后根据结果和日期进行计算。计算是有效的:
log(result).where max(date) minus log(result).where min(date)
每个名称/研究/组只有两个日期,因此最大数据(log)的结果减去最小日期(log)的结果。这是我到目前为止所尝试过的但没有运气:
var result =
from results in sortedData.AsEnumerable()
group results by results.animal
into grp
select new
{
animal = results.animal,
study = results.study,
groupNumber = results.groupNumber,
TGI = System.Math.Log(grp.Select(c => c.volume)
.Where(grp.Max(c=>c.operationDate)))
- System.Math.Log(grp.Select(c => c.volume)
.Where(grp.Min(c => c.operationDate)))
};
有人指点一下吗?谢谢。
I have some data in a List of User defined types that contains the following data:
name, study, group, result, date. Now I want to obtain the name, study and group and then a calculation based onthe result and date. The calculation is effectively:
log(result).where max(date) minus log(result).where min(date)
There are only two dates for each name/study/group, so the result from the maximum data (log) minus the result from the minumum date (log). here is what I have tried so far with no luck:
var result =
from results in sortedData.AsEnumerable()
group results by results.animal
into grp
select new
{
animal = results.animal,
study = results.study,
groupNumber = results.groupNumber,
TGI = System.Math.Log(grp.Select(c => c.volume)
.Where(grp.Max(c=>c.operationDate)))
- System.Math.Log(grp.Select(c => c.volume)
.Where(grp.Min(c => c.operationDate)))
};
Anybody any pointers? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不完全清楚分组与您的问题有何关系(在分组之后从范围变量中提取属性有什么意义?),但是您遇到困难的部分可以使用
MaxBy
和MinBy
运算符,例如 morelinq 附带的运算符。否则,您可以使用
Aggregate
模拟这些运算符,或者如果您不介意排序的低效率:It isn't entirely clear how the grouping relates to your problem (what sense does it make to extract a property from a range variable after it has been grouped?), but the part you're having difficult with can be solved easily with
MaxBy
andMinBy
operators, such as the ones that come with morelinq.Otherwise, you can simulate these operators with
Aggregate
, or if you don't mind the inefficiency of sorting:您有一些语法问题,您无法在 into grp 行之后使用 results 参数。所以我最初的尝试是像这样改变你的陈述
You have a few syntax problems, you cannot use the results parameter after your into grp line. So my initial attempt would be to change your statement like so