Calculate Maximum Size of a Value String


10/13/2015 8:29 AM

C# Development

Use C# to determine the longest value returned from device detection

The following C# code creates a data set instance from a source pattern binary uncompressed data file. A foreach loop uses Linq to iterate through each of the Components contained in the data set (currently Hardware, Operating System, Browser and Crawler) and their associated Profiles. All the values returned from the available properties for the profile are fetched and their lengths checked against the maximum length found so far. The longest values is finally output after all profiles have been checked.

int profiles = 0; int maxLength = 0; using (var dataSet = MemoryFactory.Create( @"[YOUR PATTERN BINARY DATA FILE PATH]")) { foreach(var profile in dataSet.Components.SelectMany(i => i.Profiles)) { foreach(var property in profile.Component.Properties) { var value = profile[property]; if (value != null && value.ToString().Length > maxLength) { maxLength = value.ToString().Length; } } profiles++; if (profiles % 1000 == 0) { Console.WriteLine("Processed '{0}' Profiles", profiles); } } } Console.WriteLine("Maximum value string length '{0}'", maxLength); Console.ReadKey();

This code shows how easy it is to access meta data using the 51Degrees APIs. The code could be adapted to provide values for user interfaces, provide meta data for reports, or extract device values from profile IDs stored during real time detection.

All versions of the 51Degrees data including Lite, Premium and Enterprise are supported.