1

I got a list of objects(ip, domainname). and want to find the duplicates in them and remove the ones that has not got www in front of the domainname.

So if it is in the list

192.168.0.0   www.stackoverflow.com
192.168.0.1   stackoverflow.com

I want to remove stackoverflow.com.

So far this is my code I am passing my list of objects to this function:

static List<ServerBindings> removeDuplicates(List<ServerBindings> inputList)
      {
          Dictionary<string, string> uniqueStore = new Dictionary<string, string>();
          List<ServerBindings> finalList = new List<ServerBindings>();
          foreach (var currValue in inputList)
          {
              if (!uniqueStore.ContainsKey(currValue.DomainName))
              {
                      uniqueStore.Add(currValue.DomainName, currValue.IPAddress);
                      finalList.Add(new ServerBindings { DomainName = uniqueStore.Keys.ToString(), IPAddress = uniqueStore.Values.ToString() });
              }
          }

          return finalList;
      }

I have tried linq but as I'm new to it I tried to groupby but don't know how to say "select ones where it has www in front of domain name".

EDIT:

Tested this again and seems not to work...I mean the linq query selects only the ones that have www in front and ignores the ones without....to clarify if in list we have www.test.com, test.com and test3.com the end result should be www.test.com and test3.com

martineau
  • 119,623
  • 25
  • 170
  • 301
Zaki
  • 5,540
  • 7
  • 54
  • 91
  • 1
    This is how you [**Remove duplicates in the list using linq**][1]. [1]: http://stackoverflow.com/questions/1606679/remove-duplicates-in-the-list-using-linq – Prisoner ZERO Jan 06 '12 at 17:38
  • Duplicate http://stackoverflow.com/questions/1606679/remove-duplicates-in-the-list-using-linq – 3Dave Jan 06 '12 at 17:41
  • Can you clarify exactly what you want here. if you had stackoverflow.com in the list but not www.stackoverflow.com do you want the algorithm to add in www.stackoverflow.com and use that? – Bob Vale Jan 07 '12 at 08:21
  • nope if it had stackoverflow.com and no duplicate then that should be in the list – Zaki Jan 09 '12 at 15:32
  • What object type do you want as the return value, do you want just the server bindings – Bob Vale Jan 09 '12 at 15:53
  • no worries was able to do this in a loop – Zaki Jan 10 '12 at 11:08
  • Hi Bob, tested this again and seems not to work...I mean your linq query selects only the ones that have www in front and ignores the ones without....to clarify if in list we have www.test.com, test.com and test3.com the end result should be www.test.com and test3.com – Zaki Jan 12 '12 at 09:41

3 Answers3

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

if distinct doesn't do the job because the bindings are different objects you could do

var result=from x in list
      where x.DomainName.StartsWith("www.")
      group x by x.DomainName into domain
      select new ServerBindings { 
        DomainName=domain.Key,
        IPAddress=domain.Select (d =>d.IPAddress ).First ()
      };
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
0

Something like this should do the whole thing:

serverBindings
    .Select(sb => new { Normalized = sb.DomainName.StartsWith("www.") ? sb.DomainName.Substring(4) : sb.DomainName, HasLeadingWWW = sb.DomainName.StartsWith("www."), Binding = sb })
    .GroupBy(sbn => sbn.Normalized)
    .Select(g => g.OrderBy(sbn => sbn.HasLeadingWWW).First.Binding);

NOTE: I haven't tested it, might need some tweaking.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125
  • This will add www to any domains that don't have www, the question didn't ask for this. – Bob Vale Jan 06 '12 at 18:07
  • Typo, what I meant to say was this will find domains that don't have www infront them. So if there was no www.stackoverflow.com in the list but there was a stackoverflow.com it would still find stackoverflow.com – Bob Vale Jan 06 '12 at 18:27
  • That's true- but I think that was implied in the question. He said "find the duplicates in them and remove the ones that [sic] has not got www in front of the domainname." – Chris Shain Jan 06 '12 at 19:03
  • Ah, re-reading I see your point. My code is reversed. Will edit accordingly. – Chris Shain Jan 06 '12 at 19:04
  • yes this was fine too but i have changed the design a bit can you see above please and advice – Zaki Jan 09 '12 at 15:33
  • I am not sure what the question is asking now. For what it's worth, my answer produces something very much like the data structure you have described in your added detail. – Chris Shain Jan 09 '12 at 15:36
-1
return inputList
         .GroupBy(key=>key.IpAddress)
         .Select(group => {
                 var domain = group.Any(g=>g.StartsWith("http://www"))
                    ? group.First(g=>g.StartsWith("http://www"))
                    : group.First();
                 return new ServerBindings
                    {
                       DomainName = group.First
                       IpAddress = group.Key
                    };)
         .ToList();
Jason Meckley
  • 7,589
  • 1
  • 24
  • 45
  • But this will return based on the ip address, not on the domain – Bob Vale Jan 06 '12 at 18:05
  • grouping on ip address gets you a list of domains. from that list you can select the domain, either the default if there is 1 or the domain with 'www'. then you have a 1:1 association of ip to domain. at that point you then have unique domains. – Jason Meckley Jan 06 '12 at 18:40
  • But they are not unique to the domainname, the OP has not specified that the domain to IP is 1:1, what happens if there are two domains to a single IP. The OP specified unique domains not unique IPs – Bob Vale Jan 06 '12 at 18:46