Solved

Linq Query remove duplicates from list

Posted on 2016-09-08
2
71 Views
Last Modified: 2016-09-28
Hi

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)
                                               select
                                               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}
}
0
Comment
Question by:Member_2_7967608
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points (awarded by participants)
ID: 41790692
Hi Member_2_7967608;

I am confused by your query. For example I thought that resp.transaction.quote_data.sub_documents was an array of strings. If so then array of string does not have an attributes property. I am also not sure what you are trying to do in the part of the query for scns. Please take a moment to comment the code so I can hopefully understand it and what you are trying to accomplish. Also when posting code in a comment first copy the code you are going to post then place the cursor in the text box where you want to paste it then in the menu bar of the post text box click on the icon CODE then press the CTRL-V keys to paste it. When you submit the post it will look like the code below. This will make reading your code much easier on my eyes, thanks.

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)
                               select
                               new scns()
                               {
                                   scnCode = code
                               }).GroupBy(o => o.scnCode).Select(g => g.First()).ToList()
                   }).ToList();

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41819431
Solution was posted.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question