C# 用LINQ在C中计算百分位数#

C# 用LINQ在C中计算百分位数#,c#,linq,C#,Linq,我知道有很多答案说明如何使用LINQ计算百分位数,但我找不到任何与我的问题相关的答案 我有一个对象列表,其中包含时间持续时间作为属性之一,我必须通过按名称分组列表来计算百分比。在C#中使用linq有什么最简单的方法可以做到这一点吗。 我的对象如下所示: class Performance{ public string Name { get; set; } public Nullable<decimal> DurationMilliSeconds { g

我知道有很多答案说明如何使用LINQ计算百分位数,但我找不到任何与我的问题相关的答案

我有一个对象列表,其中包含时间持续时间作为属性之一,我必须通过按名称分组列表来计算百分比。在C#中使用linq有什么最简单的方法可以做到这一点吗。 我的对象如下所示:

class Performance{
        public string Name { get; set; }
        public Nullable<decimal> DurationMilliSeconds { get; set; }
        public string typeName { get; set; }
}
类性能{
公共字符串名称{get;set;}
公共可为空的持续时间毫秒数{get;set;}
公共字符串类型名{get;set;}
}

这将构造一个字典,将
性能的实例映射到集合中所有
性能
对象的总持续时间中,按名称分组到组的
持续时间毫秒
属性的总和的百分比。假定
null
持续时间为0

var performances = Enumerable.Empty<Performance>();
var totalDuration = performances.Sum(p => p.DurationMilliSeconds ?? 0M);
var durationPercentages = performances
    .GroupBy(p => p.Name)
    .ToDictionary(
        group => group.Key,
        group => group.Sum(p => (p.DurationMilliSeconds ?? 0M))/totalDuration);
var performances=Enumerable.Empty();
var totalDuration=performances.Sum(p=>p.DurationMillistics±0M);
var持续时间百分比=绩效
.GroupBy(p=>p.Name)
.ToDictionary(
group=>group.Key,
group=>group.Sum(p=>(p.DURATIONMICLUSES??0M))/TOTALDURE);

您找到了什么样的答案,为什么不适用于您的问题?另外,澄清一下,你的意思是,对吗?你想要什么?这不是给出了百分比,而不是百分位数吗?嗯,这是真的,我假设百分比就是这个意思。我会调查一下如果同样的持续时间是多少毫秒list@NikhilVC那有什么关系?如果需要按名称排序,只需添加ThenBy()。它将始终为同一个名称指定不同的百分比values@NikhilVC,我不明白你的意思。请以问题的形式发布示例数据,说明您的意思。
Random r = new Random();
int performers = 500;
var performances = Enumerable.Range(1, performers)
  .Select(e => new Performance { Name="P"+e, DurationMilliSeconds=r.Next(5000)});
var pCount = performances.Count();
var percentiles = performances
  .OrderBy(p => p.DurationMilliSeconds)
  .Select((p, i) => new { p, i=i+1 })
  .Select(p => new {
    Name = p.p.Name,
    Duration = p.p.DurationMilliSeconds,
    Percentile = p.i / (decimal)pCount
  });