These two rows seems to do the same thing. A plussign (+) can be used instead of an anonymous type.
var newlist1 = list.GroupBy(x => x.FIELD1 + x.FIELD2).Select(y => y.First());
var newlist2 = list.GroupBy(x => new {x.FIELD1, x.FIELD2}).Select(y => y.First());
Now my question:
Is the plussign (+) something thats documented for GroupBy?
be careful of this 🙂
If for example
x.FIELD1andx.FIELD2are properties of type string, you’re just grouping by the result of concatenating the two…. which is probably not what you want. Same applies for other types of course, but an example in strings still:Given
Field1= “ABC” andField2= “DEF”, your grouping will be with the key “ABCDEF”, right?So what if you had
Field1= “AB” andField2= “CDEF”? Very much different values, but your grouping would still be “ABCDEF”…You should stick to anonymous types for grouping (when used within a method only), or when needed externally, a new class, struct, or make use of a Tuple.
EDIT: Another quick note: after you have executed the
GroupBy(without the projection), take a look at the Key values you are getting…. it should show you an example of what I mean.