C# LINQ 查询

发布于 2024-11-02 09:31:09 字数 817 浏览 1 评论 0原文

我在用户定义类型列表中有一些数据,其中包含以下数据:

名称、研究、组、结果、日期。现在我想获取姓名、研究和小组,然后根据结果和日期进行计算。计算是有效的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2024-11-09 09:31:09

目前尚不完全清楚分组与您的问题有何关系(在分组之后从范围变量中提取属性有什么意义?),但是您遇到困难的部分可以使用 MaxByMinBy 运算符,例如 morelinq 附带的运算符。

var result = from results in sortedData.AsEnumerable()
             group results by results.animal into grp
             select new
             {
                animal = grp.Key,
                study = ??,
                groupNumber = ??,
                TGI  =  Math.Log(grp.MaxBy(c => c.operationDate).volume) 
                      - Math.Log(grp.MinBy(c => c.operationDate).volume)    
              };

否则,您可以使用Aggregate模拟这些运算符,或者如果您不介意排序的低效率:

var result = from results in sortedData.AsEnumerable()
             group results by results.animal into grp

             let sortedGrp = grp.OrderBy(c => c.operationDate)
                                .ToList()
             select new
             {
                animal = grp.Key,
                study = ??,
                groupNumber = ??,
                TGI  = sortedGrp.Last().volume  - sortedGrp.First().volume               
             };

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 and MinBy operators, such as the ones that come with morelinq.

var result = from results in sortedData.AsEnumerable()
             group results by results.animal into grp
             select new
             {
                animal = grp.Key,
                study = ??,
                groupNumber = ??,
                TGI  =  Math.Log(grp.MaxBy(c => c.operationDate).volume) 
                      - Math.Log(grp.MinBy(c => c.operationDate).volume)    
              };

Otherwise, you can simulate these operators with Aggregate, or if you don't mind the inefficiency of sorting:

var result = from results in sortedData.AsEnumerable()
             group results by results.animal into grp

             let sortedGrp = grp.OrderBy(c => c.operationDate)
                                .ToList()
             select new
             {
                animal = grp.Key,
                study = ??,
                groupNumber = ??,
                TGI  = sortedGrp.Last().volume  - sortedGrp.First().volume               
             };
千と千尋 2024-11-09 09:31:09

您有一些语法问题,您无法在 into grp 行之后使用 results 参数。所以我最初的尝试是像这样改变你的陈述

var result = 
    from results in sortedData.AsEnumerable()
    group results by new
    {
     Animal = results.animal,
     Study = results.study,
     GroupNumber = results.groupNumber 
    }
    into grp
    select new
    {
         animal = grp.Key.Animal,
         study = grp.Key.Study,
         groupNumber = grp.Key.GroupNumber,
         TGI = System.Math.Log(grp.OrderByDescending(c=>c.operationDate).First().volume)

             - System.Math.Log(grp.OrderBy(c=>c.operationDate).First().volume)
    };

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

var result = 
    from results in sortedData.AsEnumerable()
    group results by new
    {
     Animal = results.animal,
     Study = results.study,
     GroupNumber = results.groupNumber 
    }
    into grp
    select new
    {
         animal = grp.Key.Animal,
         study = grp.Key.Study,
         groupNumber = grp.Key.GroupNumber,
         TGI = System.Math.Log(grp.OrderByDescending(c=>c.operationDate).First().volume)

             - System.Math.Log(grp.OrderBy(c=>c.operationDate).First().volume)
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文