Collecting the data into one object and sorting it in .NET/C#
In our .NET/C# project we save all the users' browser name/version, OS name, etc. in the database. Later on we grab tha开发者_如何转开发t data and use for statistics purposes. Now I need to somehow define, for example:
5 customers had Windows 7 + Internet Explorer 8
4 customers had Windows XP + Internet Explorer 6 etc.When getting the data from the database, I can define which OS and which browser was used, but the case is that when I collect the data, how would I get which browser and which OS were used together and get the quantity of such platforms? For example I would search Windows XP and the system would show me which browsers were used with that OS and in what quantity.
Could anybody give a hint?
Thanks.
Sounds like a perfect fit for the GROUP BY
operator (if you're using SQL), or the Linq GroupBy()
method (http://msdn.microsoft.com/en-us/library/system.linq.enumerable.groupby.aspx) if you're wanting to stay within .NET.
For example, in SQL:
SELECT COUNT(*), OS, BROWSER
FROM YOURTABLE
GROUP BY OS, BROWSER
Or, in Linq:
var groups = from yt in yourtable
group yt by new { yt.OS, yt.BROWSER } into ytgroup
select new { key = ytgroup.key, groups = ytgroup };
From here, you'll have a set of groups, from which you can then obtain the counts.
You need to look at grouping. In linq that might look something like:
var info = from record in records where record.OperatingSystem == "WinXp"
group record by record.Browser into g
select new { Browser = g.Key, Records = g };
If you use something like ravendb you will have a very nice way of doing this without the complicated impedance missmatch that SQL RDBMS inherantly brings. Meaning you can query it directly with this linq.
hth
Create Dictionary<string, int> d;
Let your key be strings such as 'Windows 7 + Internet Explorer 8' or 'Windows XP + Internet Explorer 6'. You will probably have your key generation routine ready.
Do this:
foreach (CollectedDataItem item in CollectedData)
{
string key=generateKeyFromItem(item);
if (!d.HasKey(key)) {
d.Add(key, 1);
}
else {
d[key]++;
}
}
And finally,
string generateKeyFromItem(CollectedDataItem Item)
{
return Item.OS+" "+Item.Browser;
}
精彩评论