Hi,

Want to remove duplicates from a list so if my list contains:

www.test.com
test.com
mytest.com

I want the final list to look like below(only selecting the domains with www from the duplicate in front) :

www.test.com
mytest.com

I have this linq but it seems to ignore all the domains which dont have www in front because it is selecting only www ones:

var result=inputList.Where(x=>x.DomainName.StartsWith("www.")).Distinct();

help would be appreciated

You could do it this way, but this makes some assumptions:

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_406156_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         List<string> lst_strInput = new List<string>()
         {
            "www.test.com", "test.com", "mytest.com"
         };

         lst_strInput
            .Select(s => s.Replace("www.", ""))
            .Distinct()
            .ToList()
            .ForEach(s => Console.WriteLine(s));
      }
   }
}

Will you ever need to distinguish between fred.wordpress.com and joe.wordpress.com?
...AND are you dealing with URIs or strings?

OK. Then that will work.
Just for grins, here is the Uri version which assumes EITHER the host with the www or without the www will work as long as both don't appear:

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_406156_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         List<Uri> lst_uriInput = new List<Uri>()
         {
            new Uri("http://www.test.com"),
            new Uri("http://test.com"),
            new Uri("http://mytest.com")
         };

         List<Uri> lst_uriFiltered =
            lst_uriInput.ToLookup(k => k.Host.Replace("www.", ""), v => v)
            .Select(kvp => kvp.First())
            .ToList();

         lst_uriFiltered.ForEach(uri => Console.WriteLine(uri.Host));
      }
   }
}

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.