Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C按双精度列表对字符串列表进行排序_C#_Linq - Fatal编程技术网

C# C按双精度列表对字符串列表进行排序

C# C按双精度列表对字符串列表进行排序,c#,linq,C#,Linq,我需要创建一个字符串列表,这些字符串表示求和值的前五位 我有一个数据库,里面有数百张不同服务的账单 2013年1月电气价格600美元 2013年1月50美元的水费 我需要对所有相同的服务进行汇总 这是我在这里做的 public List<Double> GetSumOfSingleServices { get { var sums = (from dc in GetDashboardData

我需要创建一个字符串列表,这些字符串表示求和值的前五位

我有一个数据库,里面有数百张不同服务的账单

2013年1月电气价格600美元 2013年1月50美元的水费

我需要对所有相同的服务进行汇总 这是我在这里做的

public List<Double> GetSumOfSingleServices
    {
        get
        {

            var sums = (from dc in GetDashboardData                            
                        group dc by dc.serviceType into g
                        select g.Sum(sc => sc.serviceCost)).ToList();

            return sums;
        }
        set
        {
            NotifyPropertyChanged("GetSumOfSingleServices");
        }

    }
我用下面的代码创建了一个字符串列表

public List<String> GetServiceNames
    {
        get
        {

            var names = (from dc in GetDashboardData
                         group dc by dc.serviceType into g                             
                         select g.First().serviceType).ToList();

            return names;
        }
        set
        {
            NotifyPropertyChanged("GetServiceNames");
        }
    }
现在这两个列表中的数据是平行的 GetSumOfSingleServices[0]是GetServiceNames[0]等的值

我希望有一个列表,其中字符串按GetSumOfSingleServices first中的最高值排序,以此类推

因此,如果最高的GetSumOfSingleServices[3]及其并行字符串是GetServiceNames[3],那么我希望GetServiceNames[3]是列表中的第一个条目

不确定如何按双值对字符串列表进行排序


对于一般问题,我将使用,Perl中使用的通用模式。这应该很容易理解

您的情况更简单,因为您可以完全控制数据访问:

var tuples = from dc in GetDashboardData
            group dc by dc.serviceType into g
            select new{
                Cost = g.Sum(sc=>sc.serviceCost),
                Type = g.Key,
            };
var sorted = tuples.OrderByDescending(t=>t.Cost).Select(t=>t.Type);
return sorted.ToList();

有了这样的源代码,您的所有转换都变得简单:

var compositeList = GetDashboardData
     .GroupBy(dc => dc.serviceType)
     .Select(g => new{
                    name = g.Key, 
                    sum = g.Sum(sc => sc.serviceCost)})
     .ToList();

您可以考虑用两个属性名称和SUM来构造一个具体的类,而不是上面的匿名对象声明。 现在:

等等


同步列表很糟糕。

您可以对正在查询的类实现IComparable接口,也可以创建一个新类来实现IComparaer接口,并将其作为参数传递给排序方法。

为什么不创建一个包含名称和总和的复合对象的单一列表?那就非常容易分类了。同步索引闻起来很糟糕。完全同意同步列表是一场噩梦。如果数据在GetSumOfSingleServices和GetServiceNames之间更改,或者如果数据以您不期望的顺序返回,那么一切都结束了。这是以一致和可预测的方式获取所需信息的完美方式。
compositeList.OrderByDescending(x => x.sum).Select(x => x.name);