We help IT Professionals succeed at work.
Get Started

Linq Query remove duplicates from list

Last Modified: 2016-09-28

I had question related to this Refactor C# code.

The source classes for DTO's are the in the above question. I want to remove duplicates created in salesorders List based on productCode ( mapping values[1] )

The issue is each List has as child list variable called scns (values[5]) which also needs to be created from the source data

 req.salesOrders = (from docArray in resp.transaction.quote_data.sub_documents
                                   let document = Regex.Split(docArray, @"\$,\$")
                                   let values = document.attributes[0].value.ToString().Split(new[] { "**" }, StringSplitOptions.None)
                                   where (document.attributes != null && document.attributes.Length > 0) && !string.IsNullOrWhiteSpace(document.attributes[0].value.ToString()) && !string.IsNullOrEmpty(values[3]) && (!string.IsNullOrWhiteSpace(values[5]) && values[5] != "$$")
                                   select new salesOrder()
                                       productCode = values[1],
                                       salesOrderType = values[2],
                                       startDate = DateTime.Parse(values[3]).ToString("yyyy-MM-dd"),
                                       pays = !string.IsNullOrWhiteSpace(values[4]) ? int.Parse(values[4]) : 0,
                                       scns = (from code in values[5].Split(new[] { "$$" }, StringSplitOptions.RemoveEmptyEntries)
                                               new scns()
                                                   scnCode = code
                                               }).GroupBy(o => o.scnCode).Select(g => g.First()).ToList()

                                   }).ToList() ;

Example req.salesOrders  should have single record for req.salesOrders=10  (product) this should be unique.   All the req.scns

> 1**10**New Client**2016-11-02**250**F0040Z$$F0040Z$$F00141$,$1**,1W**New Client - Add2 Product**2016-11-02**250**F71900$$F0070X

> 1**10**New Client**2016-11-02**250**F00498$$F00493$$F00141

Example in above it should return
productCode = 10
 salesOrderType =New Client,
 startDate = '2016-11-02',
 pays = 250
 scns = {F0040Z,F00141, F00498,F00493}  //removed dups . this is implemented
productCode = 1W
 salesOrderType =New Client - Add2 Product,
 startDate = '2016-11-02',
 pays = 250
 scns = { F71900,F0070X}
Watch Question
Distinguished Expert 2017
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE