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

7
  • 1
    This is how you [Remove duplicates in the list using linq][1]. [1]: stackoverflow.com/questions/1606679/… Commented Jan 6, 2012 at 17:38
  • Duplicate stackoverflow.com/questions/1606679/… Commented Jan 6, 2012 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? Commented Jan 7, 2012 at 8:21
  • nope if it had stackoverflow.com and no duplicate then that should be in the list Commented Jan 9, 2012 at 15:32
  • What object type do you want as the return value, do you want just the server bindings Commented Jan 9, 2012 at 15:53

3 Answers 3

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 ()
      };
Sign up to request clarification or add additional context in comments.

3 Comments

That does not remove duplicates.
@TimSchmelter Yes, it was a typo, meant to hit submit later
Hi Bob: this worked but I have changed the design of application a little bit.. see above for details can you help with this please.
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.

6 Comments

This will add www to any domains that don't have www, the question didn't ask for this.
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
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."
Ah, re-reading I see your point. My code is reversed. Will edit accordingly.
yes this was fine too but i have changed the design a bit can you see above please and advice
|
-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();

3 Comments

But this will return based on the ip address, not on the domain
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.