LINQ Group在VB.Net中具有多个属性 [英] LINQ Group by with multiple properties in VB.Net

查看:118
本文介绍了LINQ Group在VB.Net中具有多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个问题上花了很多时间。我能够做简单的Group LINQ查询(在一个属性上),但对于多个字段我有点卡住...
这是我想要做的LINQPad示例:

  dim lFinal = {new with {.Year = 2010,.Month = 6,.Value1 = 0,.Value2 = 0},
以{.Year = 2010,.Month = 6,.Value1 = 2,.Value2 = 1},
new与{.Year = 2010,.Month = 7,.Value1 = 3,.Value2 = 4 },
new with {.Year = 2010,.Month = 8,.Value1 = 0,.Value2 = 1},
new with {.Year = 2011,.Month = 1,.Value1 = 2,.Value2 = 2},
new with {.Year = 2011,.Month = 1,.Value1 = 0,.Value2 = 0}}

Dim lFinal2 = From el In lFinal
Group el按键=新增{el.Year,el.Month}
进入组
用{.Year = Key.Year,.Month = Key.Month,。 Value1 = Group.Sum(Function(x)x.Value1),.Value2 = Group.Sum(Function(x)x.Value2)}

lFinalDump()
lFinal2。转储()

lF inal list has 6 items,I want the lFinal2 to have 4 items:2010-6 and 2011-1 should group。

在此先感谢。

解决方案

不是100%确定,但group可能使用Equals()和/或GetHashCode实现,所以当您执行隐式创建时:

  = Group el By Key = new with {el.Year,el.Month} 

隐式对象不知道检查年份和月份(仅仅因为它具有属性并不意味着它会在与其他对象比较时检查它们)。



所以你可能需要做更多的事情:

  = Group el By Key = new CustomKey(){Year = el.Year,Month = el.Month}; 

公共类CustomKey {
int年份{get;组; }
int Month {get;组; }

public override bool Equals(obj A){
var key(CustomKey)A;
return key.Year == this.Year&& key.Month == this.Month;
}
}


I spent a lot of time on this problem. I am able to do simple Group By LINQ queries (on one property) but for multiple fields I'm a little stuck... Here is a LINQPad sample of what I want to do :

dim lFinal={new with {.Year=2010, .Month=6, .Value1=0, .Value2=0}, 
            new with {.Year=2010, .Month=6, .Value1=2, .Value2=1},
            new with {.Year=2010, .Month=7, .Value1=3, .Value2=4},
            new with {.Year=2010, .Month=8, .Value1=0, .Value2=1},
            new with {.Year=2011, .Month=1, .Value1=2, .Value2=2},
            new with {.Year=2011, .Month=1, .Value1=0, .Value2=0}}

Dim lFinal2 = From el In lFinal
              Group el By Key = new with {el.Year,el.Month}
              Into Group
              Select New With {.Year = Key.Year, .Month=Key.Month, .Value1 = Group.Sum(Function(x) x.Value1), .Value2 = Group.Sum(Function(x) x.Value2)}

lFinal.Dump()
lFinal2.Dump()

The lFinal list has 6 items, I want the lFinal2 to have 4 items : 2010-6 and 2011-1 should group.

Thanks in advance.

解决方案

Not 100% sure but group by probably uses the Equals() and/or GetHashCode implementation, so when you do the implicit creation:

= Group el By Key = new with {el.Year,el.Month}

the implicit object doesn't know to check both year and month (just because it has the properties doesn't mean it checks them when comparing to other objects).

So you'll probably need to do something more like this:

= Group el By Key = new CustomKey() { Year = el.Year, Month = el.Month };

public class CustomKey{
    int Year { get; set; }
    int Month { get; set; }

    public override bool Equals(obj A) {
        var key (CustomKey)A;
        return key.Year == this.Year && key.Month == this.Month;
    }
}

这篇关于LINQ Group在VB.Net中具有多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆