• About Us
  • Blog
  • Basket
  • Account
  • Sign In
  •  

Blog

Published on Tuesday, October 13, 2015

Calculate Maximum Size of a Value String

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.

Compare Data Options

Comments (0)

Author: Products Team

Categories: Development

Tags: C# , Meta Data

James Rosewell Host
>

Products Team

Other posts by Products Team
Contact author

Name:
Email:
Subject:
Message:
x