-1

I have an arrayList with below data in C#.net so I want to check uniqueness for only 3 items in the stings in arraylist items .uniqueness over ID,date,name "ID;date time;name;code" ofcourse I want only date section of datetime to be unique not both date and time section

"1;06/07/2017 11:17:16;out;0"
"1;06/07/2017 11:27:16;out;2"
"1;06/07/2017 11:28:16;out;0"
"1;06/07/2017 11:20:16;out;3"

so I want to distinct this arraylist because of duplicate date and ID and out please help me to solve this.

Shayan
  • 45
  • 8
  • 2
    This really isn't code, this is just a sample of your data. What have you tried already? – maccettura Jun 14 '17 at 19:10
  • dear maccettura : i could not find any solution for this problem – Shayan Jun 14 '17 at 19:12
  • So [you've tried nothing and you're already out of ideas](https://www.youtube.com/watch?v=QG8Yr5tkyTg&feature=youtu.be&t=11s)? – maccettura Jun 14 '17 at 19:14
  • ArrayLists exist in C# for backwards compatibility only. There is no valid reason to ever use them in new code. Use a generic List instead. – Joel Coehoorn Jun 14 '17 at 19:20
  • Possible duplicate of [Remove duplicates in the list using linq](https://stackoverflow.com/questions/1606679/remove-duplicates-in-the-list-using-linq) – KillerIsDead Jun 14 '17 at 19:30

1 Answers1

1

The best way to do it is using LINQ. I would highly recommend using a List<string> instead of ArrayList. You can do distinct in the following way

using System.Collections;
using System.Linq;

ArrayList list = new ArrayList() {
    "1;06/07/2017 11:17:16;out;0",
    "1;06/07/2017 11:27:16;out;2",
    "1;06/07/2017 11:28:16;out;0",
    "1;06/07/2017 11:20:16;out;3"
};

var distinctEnumerable = list.Cast<string>()
    .GroupBy(x => {
        var items = x.Split(';');
        var dateParts = items[1].Split(' ');
        return string.Join(";", items[0], dateParts[0], items[2]);
    })
    .Select(x => x.Key);

You can convert the IEnumerable<string> to List<string> or an string[] or some other convenient container if you wish to.

The main logic here is that, I GroupBy the List using the first 3 parts which you need distinct and then select the Key of the group, which will give us the distinct list.

Live Code Demo Here

Vikhram
  • 4,294
  • 1
  • 20
  • 32
  • dear Vikhram : only 3 elements are important. ID ,date and string. if these items are the same .duplicated items must be removed – Shayan Jun 14 '17 at 19:21
  • @ Vikhram :OFcourse I want date section of datetime not both date and time – Shayan Jun 14 '17 at 19:39
  • @ Vikhram I tested your code it returns 2 items.but I expected only 1 item because items are "1;06/07/2017 11:17:16;out;0" "1;06/07/2017 11:27:16;out;2" "1;06/07/2017 11:28:16;out;0" "1;06/07/2017 11:20:16;out;3" and all IDs and date parts and names are the same – Shayan Jun 14 '17 at 19:51
  • @Vikharm I want the result to be only "1;06/07/2017 11:17:16;out;0" – Shayan Jun 14 '17 at 19:54
  • I am running your code and see the result .it returns 2 items I am sure – Shayan Jun 14 '17 at 19:56
  • @Shayan Probably. You should be able to see the logic I have built and adapt it to your situation, potentially by parsing it differently – Vikhram Jun 14 '17 at 20:04