如何使用linq获得此结果

时间:2013-12-24 07:47:32

标签: c# linq

我收集了具有以下结构的物品

var results= [

{3           2013        5644.57127629528},
{9           2013        5352.08258069403},

{1           2013        5644.81057603413},
{10          2013        170.013936031616},
{4           2013        5383.75537735399},
{6           2013        5413.9359165467},
{7           2013        5625.75008073717},
{2           2013        5015.54442930293},
{8           2013        5534.53627550067},
{5           2013        5538.10984479548},
{4           2012        5438.81051077828},
{1           2012        5585.21099483651}
{11          2012        5325.79426499073}
{12          2012        5683.07184047395},
{6           2012        5366.00634837989},
{7           2012        5580.53474782527},
{3           2012        5615.08714653137},
{9           2012        5435.35217403032},
{8           2012        5579.6929459533},
{2           2012        5175.35027960843}
{10          2012        5556.99197086551},
{5           2012        5575.47039317314},
{10          2011        5512.31976625987},
{12          2011        5669.3365148545},
{11          2011        5356.42306696735}

]

月,年,Totalkwh(usageModel)(对象值的表格表示,实际上我的记忆中有这些值的对象集合)

从这个集合中我想获得看起来像以下结构的结果集

[{10          2013        170.013936031616    10          2012        5556.99197086551},{9     2013   5352.08258069403    9           2012        5435.35217403032  },

{8           2013        5534.53627550067   8           2012        5579.6929459533}, {7           2013        5625.75008073717    7           2012        5580.53474782527},

{6           2013        5413.9359165467    6           2012        5366.00634837989} ,{5           2013        5538.10984479548   5           2012        5575.47039317314},

{4           2013        5383.75537735399   4           2012        5438.81051077828},{3           2013        5644.57127629528  3           2012        5615.08714653137},

{2           2013        5644.81057603413  2           2012        5175.35027960843}, {1           2013        5644.81057603413, 1           2012        5585.21099483651},

{12          2012        5683.07184047395    12          2011        5669.3365148545 },{11          2012        5325.79426499073, 11          2011        5356.42306696735},

{10          2012        5556.99197086551  10          2011        5512.31976625987 }] 

leftMonth,leftYear,leftValue rightMonth,rightYear,rightValue(reportModel)

主要注意事项:请查看基于月份和两个成员年份分组的数据形成左右部分的方式

我如何使用linq或任何其他机制来做到这一点

3 个答案:

答案 0 :(得分:0)

这是一种简单的方法,尽管不是最有效的方法: (这将在下一年没有匹配月份的月份中返回-1作为rightValue。如果您不需要,可以在之后过滤掉那些)

var groupedResults = results.Select(r=> new 
    {
        leftMonth = r.Month, 
        leftYear = r.Year, 
        leftKwh = r.Totalkwh, 
        rightMonth = r.Month, 
        rightYeahr = r.Year+1, 
        rightKwh = results
            .Where(r2=>r2.Year == r.Year + 1 && r2.Month == r.Month)
            .Select(r2=>rs.totalkwh)
            .DefaultIfEmpty(-1)
            .First(),
    });

results.Select(r=>

我假设您的对象包含int年份和月份(尽管您可能需要考虑使用DateTime

答案 1 :(得分:0)

我担心没有直接的LINQ查询来做你想做的事情。 BTW上面的答案不会产生你想要的结果!无论如何也要看看这个答案。

public class Structure
{
    public int Month;
    public int Year;
    public double Totalkwh;
}

public class CombinedStructure
{
    public int LM;
    public int LY;
    public double LT;
    public int RM;
    public int RY;
    public double RT;
}
        List<Structure> structures = new List<Structure>();
        structures.Add(new Structure() { Month = 3, Year = 2013, Totalkwh = 5644.57127629528 });
        structures.Add(new Structure() { Month = 9, Year = 2013, Totalkwh = 5352.08258069403 });
        structures.Add(new Structure() { Month = 1, Year = 2013, Totalkwh = 5644.81057603413 });
        structures.Add(new Structure() { Month = 10, Year = 2013, Totalkwh = 170.013936031616 });
        structures.Add(new Structure() { Month = 4, Year = 2013, Totalkwh = 5383.75537735399 });
        structures.Add(new Structure() { Month = 6, Year = 2013, Totalkwh = 5413.9359165467 });
        structures.Add(new Structure() { Month = 7, Year = 2013, Totalkwh = 5625.75008073717 });
        structures.Add(new Structure() { Month = 2, Year = 2013, Totalkwh = 5015.54442930293 });
        structures.Add(new Structure() { Month = 8, Year = 2013, Totalkwh = 5534.53627550067 });
        structures.Add(new Structure() { Month = 5, Year = 2013, Totalkwh = 5538.10984479548 });
        structures.Add(new Structure() { Month = 4, Year = 2012, Totalkwh = 5438.81051077828 });
        structures.Add(new Structure() { Month = 1, Year = 2012, Totalkwh = 5585.21099483651 });
        structures.Add(new Structure() { Month = 11, Year = 2012, Totalkwh = 5325.79426499073 });
        structures.Add(new Structure() { Month = 12, Year = 2012, Totalkwh = 5683.07184047395 });
        structures.Add(new Structure() { Month = 6, Year = 2012, Totalkwh = 5366.00634837989 });
        structures.Add(new Structure() { Month = 7, Year = 2012, Totalkwh = 5580.53474782527 });
        structures.Add(new Structure() { Month = 3, Year = 2012, Totalkwh = 5615.08714653137 });
        structures.Add(new Structure() { Month = 9, Year = 2012, Totalkwh = 5435.35217403032 });
        structures.Add(new Structure() { Month = 8, Year = 2012, Totalkwh = 5579.6929459533 });
        structures.Add(new Structure() { Month = 2, Year = 2012, Totalkwh = 5175.35027960843 });
        structures.Add(new Structure() { Month = 10, Year = 2012, Totalkwh = 5556.99197086551 });
        structures.Add(new Structure() { Month = 5, Year = 2012, Totalkwh = 5575.47039317314 });
        structures.Add(new Structure() { Month = 10, Year = 2011, Totalkwh = 5512.31976625987 });
        structures.Add(new Structure() { Month = 12, Year = 2011, Totalkwh = 5669.3365148545 });
        structures.Add(new Structure() { Month = 11, Year = 2011, Totalkwh = 5356.42306696735 });

        List<CombinedStructure> combinedStructures = new List<CombinedStructure>();
        foreach (var groupedStructure in structures.OrderByDescending(s => s.Year).GroupBy(s => s.Month))
        {
            var orderedGroup = groupedStructure.GetEnumerator();
            orderedGroup.MoveNext();
            while (true)
            {
                Structure firstStructure = orderedGroup.Current;
                Structure secondStructure;
                if (orderedGroup.MoveNext())
                {
                    secondStructure = orderedGroup.Current;
                    combinedStructures.Add(
                        new CombinedStructure()
                        {
                            LM = firstStructure.Month,
                            LY = firstStructure.Year,
                            LT = firstStructure.Totalkwh,
                            RM = secondStructure.Month,
                            RY = secondStructure.Year,
                            RT = secondStructure.Totalkwh
                        }
                    );
                }
                else
                {
                    break;
                }
            }
        }

为方便起见,我创建了两个强类型类(StructureCombinedStructure),并将数据插入Structure列表中。希望这会有所帮助。

答案 2 :(得分:0)

这是另一种方式。我打电话给班级拿着每组数据Usage。一旦数据在List中,我就使用linq表达式首先按月升序,然后按年递减。按月分组并将每个组投射到一个列表然后将整个结果转换为列表,我们最终得到一个2d列表,List&gt;按月排序,然后逐年下降:

List<Usage> results = new List<Usage>
{
    {new Usage{Month = 3, Year = 2013, Kwh = 5644.57127629528}},
    {new Usage{Month = 9, Year = 2013 , Kwh =  5352.08258069403}},
    {new Usage{Month = 1, Year = 2013, Kwh = 5644.81057603413}},
    {new Usage{Month = 10, Year = 2013, Kwh = 170.013936031616}},
    {new Usage{Month = 4, Year = 2013, Kwh = 5383.75537735399}},
    {new Usage{Month = 6, Year = 2013, Kwh = 5413.9359165467}},
    {new Usage{Month = 7, Year = 2013, Kwh = 5625.75008073717}},
    {new Usage{Month = 2, Year = 2013, Kwh = 5015.54442930293}},
    {new Usage{Month = 8, Year = 2013, Kwh = 5534.53627550067}},
    {new Usage{Month = 5, Year = 2013, Kwh = 5538.10984479548}},
    {new Usage{Month = 4 , Year = 2012, Kwh = 5438.81051077828}},
    {new Usage{Month = 1, Year = 2012, Kwh = 5585.21099483651}},
    {new Usage{Month = 11, Year = 2012, Kwh = 5325.79426499073}},
    {new Usage{Month = 12, Year = 2012, Kwh = 5683.07184047395}},
    {new Usage{Month = 6, Year = 2012, Kwh = 5366.00634837989}},
    {new Usage{Month = 7, Year = 2012, Kwh = 5580.53474782527}},
    {new Usage{Month = 3, Year = 2012, Kwh = 5615.08714653137}},
    {new Usage{Month = 9, Year = 2012, Kwh = 5435.35217403032}},
    {new Usage{Month = 8, Year = 2012, Kwh = 5579.6929459533}},
    {new Usage{Month = 2, Year = 2012, Kwh = 5175.35027960843}},
    {new Usage{Month = 10, Year = 2012, Kwh = 5556.99197086551}},
    {new Usage{Month = 5, Year = 2012, Kwh = 5575.47039317314}},
    {new Usage{Month = 10, Year = 2011, Kwh = 5512.31976625987}},
    {new Usage{Month = 12, Year = 2011, Kwh = 5669.3365148545}},
    {new Usage{Month = 11, Year = 2011, Kwh = 5356.42306696735}}
};
List<List<Usage>> newresults = (from u in results.OrderBy(x => x.Month).ThenByDescending(y => y.Year)
    group u by u.Month into g
    select g.ToList()).ToList();

public class Usage
{
    public int Year = 0;
    public int Month = 0;
    public double Kwh = 0;
    public override string ToString()
    {
        return "Month = " + Month.ToString() + 
            ", Year = " + Year.ToString() + 
            ", Kwh = " + Kwh.ToString();
    }
}

使用ToString覆盖,您可以根据需要格式化输出:

?newresults[0]
Count = 2
    [0]: {Month = 1, Year = 2013, Kwh = 5644.81057603413}
    [1]: {Month = 1, Year = 2012, Kwh = 5585.21099483651}
?newresults[1]
Count = 2
    [0]: {Month = 2, Year = 2013, Kwh = 5015.54442930293}
    [1]: {Month = 2, Year = 2012, Kwh = 5175.35027960843}
?newresults[2]
Count = 2
    [0]: {Month = 3, Year = 2013, Kwh = 5644.57127629528}
    [1]: {Month = 3, Year = 2012, Kwh = 5615.08714653137}
?newresults[3]
Count = 2
    [0]: {Month = 4, Year = 2013, Kwh = 5383.75537735399}
    [1]: {Month = 4, Year = 2012, Kwh = 5438.81051077828}
?newresults[4]
Count = 2
    [0]: {Month = 5, Year = 2013, Kwh = 5538.10984479548}
    [1]: {Month = 5, Year = 2012, Kwh = 5575.47039317314}