我的数据表(DeviceInfo):
ID | OS | Device ----------------------------------------------- 1 | Android 2.2 | Samsung 2 | Linux/Android 4.2 | LG 3 | Linux/Android 4.4.2 | HTC 4 | Android 3.2 | Samsung 5 | Android 3.0 | Motorola 6 | iOS 7.1.2 | iPad 7 | iOS 8.0 | iPhone 6 8 | iOS 6.1.6 | iPhone 4
我想通过使用Linq的Android和ios用户对该表进行分组。实际上,我必须使用子字符串“ Android”和“ iOS”对表进行分组。 我的输出应该是
ID | User | Count ---------------------------- 1 | Android | 5 2 | iOS | 3
我怎样才能使用linq获取此表?
您可以尝试这样的事情:
// db is my datacontext var groupByOS = (from c in (from d in db.DeviceInfo where d.Os.ToUpper().Contains("ANDROID") || d.Os.ToUpper().Contains("IOS") group d by new { d.Os } into dev select new { User = dev.Key.Os.ToUpper().Contains("ANDROID") ? "Android" : "iOS", DeviceCount = dev.Count() }) group c by new { c.User } into newgrp select new { newgrp.Key.User, Count = newgrp.Sum(q => q.DeviceCount) }).ToList();