score:2

Accepted answer

The Count function just:

Returns the number of elements in a sequence.

What you want to do is count the number of items in the sequence that match a certain condition. For that use the override the accepts a predicate:

.Select(g => new AppVersionModel {
    DateYYMMDD = g.Key,
    Android043 = g.Count(i => i.Android043 == 1),
    Android044 = g.Count(i => i.Android044 == 1),
    // ....
})

As you made sure the properties have 0,1 values you could also just use Sum:

.Select(g => new AppVersionModel {
    DateYYMMDD = g.Key,
    Android043 = g.Sum(i => i.Android043),
    Android044 = g.Sum(i => i.Android044),
    // ....
})

However I'd go with a different approach of having the properties store booleans as seen below. In addition no need to write x.BOOLEAN_PROPERTY == true, just write x.BOOLEAN_PROPERTY.

.Select(x => new AppVersionModel {
    DateYYMMDD = x.DateYYMMDD,
    Android043 = x.IsAndroid && x.AppVersion == "0.4.3",
    Android044 = x.IsAndroid && x.AppVersion == "0.4.4",
    Android050 = x.IsAndroid && x.AppVersion == "0.5.0",
    IOS043 = x.IsIos && x.AppVersion == "0.4.3",
    IOS044 = x.IsIos && x.AppVersion == "0.4.4",
    IOS050 = x.IsIos && x.AppVersion == "0.5.0",
})
.GroupBy(x => x.DateYYMMDD)
.OrderBy(g => g.Key)
.Select(g => new AppVersionModel {
    DateYYMMDD = g.Key,
    Android043 = g.Count(i => i.Android043),
    Android044 = g.Count(i => i.Android44),
    // ...
})

Related Articles