lets say I have a list with items :
TEST1 -- 99,56$ -- 25 PCS -- 10:56
TEST2 -- 56,57$ -- 57 PCS -- 11:01
TEST3 -- 56,57$ -- 43 PCS -- 11:06
TEST4 -- 56,57$ -- 59 PCS -- 11:33
TEST3 -- 56,57$ -- 43 PCS -- 11:06
TEST3 -- 56,57$ -- 43 PCS -- 11:06
TEST1 -- 58,98$ -- 25 PCS -- 11:45
And I would want to get only duplicated items where the NAME Value (NAME Value is the first part where TEST is) and PCS Value is the same, into a new list, so my desired new list output would be:
TEST3 -- 56,57$ -- 43 PCS -- 11:06
TEST1 -- 99,56$ -- 25 PCS -- 10:56
TEST1 -- 58,98$ -- 25 PCS -- 11:45
MY CURRENT CODE:
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
List<string> listwithbuys = new List<string>(new string[] {
"TEST1 -- 99,56$ -- 25 PCS -- 10:56",
"TEST2 -- 56,57$ -- 57 PCS -- 11:01",
"TEST3 -- 56,57$ -- 43 PCS -- 11:06",
"TEST4 -- 56,57$ -- 59 PCS -- 11:33",
"TEST3 -- 56,57$ -- 43 PCS -- 11:06",
"TEST3 -- 56,57$ -- 43 PCS -- 11:06",
"TEST1 -- 58,98$ -- 25 PCS -- 11:45" });
List<String> duplicates = listwithbuys.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
listbox1.DataSource = duplicates;
}
}
}
Above code would print me:
TEST3 -- 56,57$ -- 43 PCS -- 11:06
Which is not my goal, like I said I only want it to give me duplicated items where NAME and PCS Value are the same. Thanks to anyone trying to help :)
Can you please try this code, let me know if have any queries.
List<string> listwithbuys = new List<string>(new string[] {
"TEST1 -- 99,56$ -- 25 PCS -- 10:56",
"TEST2 -- 56,57$ -- 57 PCS -- 11:01",
"TEST3 -- 56,57$ -- 43 PCS -- 11:06",
"TEST4 -- 56,57$ -- 59 PCS -- 11:33",
"TEST3 -- 56,57$ -- 43 PCS -- 11:06",
"TEST3 -- 56,57$ -- 43 PCS -- 11:06",
"TEST1 -- 58,98$ -- 25 PCS -- 11:45" });
List<String> duplicates = listwithbuys.GroupBy(x => new { Name = x.Split(new string[] { " -- " }, StringSplitOptions.None)[0],
PCS = x.Split(new string[] { " -- " }, StringSplitOptions.None)[2] })
.Where(g => g.Count() > 1)
.Select(g => g.LastOrDefault())
.ToList();