-1

I have a key-value pair array that contains an LDAP distinguished name, and I want to retrieve the DNS domain name of the host. (Only the DC's not the fqdn)

Assume that the LDAP parsing is done correctly, and the DC entries when combined constitute the DNS domain name of the host.

Given the code below, is it possible to convert

DC = my
DC = domain
DC = com

into

my.domain.com

I could use a for...each with a stringbuilder but it doesn't feel elegant. Is there a better way?

My code is below:

var kvList = ParseDistinguishedName(ldapName);
StringBuilder sb = new StringBuilder();
var names = (from k in kvList
             where k.Key == "DC"
              select k.Value);
Ran Dom
  • 315
  • 5
  • 13

2 Answers2

3

Very easily, fortunately: string.Join does exactly what you want:

var dotSeparated = string.Join(".", names);

You might want to consider using method calls rather than a query expression for such a simple query, mind you. What you've got is precisely equivalent to:

var names = kvList.Where(k => k.Key == "DC").Select(k => k.Value);

The query expression is fine, but can end up being verbose for simple queries. (Query expressions really shine with let, join etc - anything that introduces transparent identifiers.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
-1

Probably something like this:

kvList.Where(x => x.Key == "DC")
.Select(x => x.Value)
.Aggregate((x,y) => x + "." + y)
Th0rndike
  • 3,406
  • 3
  • 22
  • 42