[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Linq Query Add new object array

Posted on 2016-09-07
10
Medium Priority
?
132 Views
Last Modified: 2016-09-08
Hi

I had question on this Refactor C# code.

How do I query this if we have more than Single record for SalesOrderType(Mapping SubDocuments.Attributes) sepertaed by "$,$"

E.g. if we have document.attributes[0 ].value as below
Note we have 2 records in this  separated by $,$

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


     req.salesOrders = (from document in resp.transaction.quote_data.sub_documents
                                   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() ;
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
  • 5
  • 5
10 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 41789422
Hi;

Linq query like this will do what you want. You will need to implement it to your code.
string[] documents = {"1**10**New Client**2016-11-02**250**10-F0040Z$$10-F0040Z$$10-F00141$,$1W**New Client - Add2 Product**2016-11-02**250**1W-F71900$$1W-F0070X",
			"1**10**New Client**2016-11-02**250**10-F0040Z$$10-F0040Z$$10-F00141$,$1W**New Client - Add2 Product**2016-11-02**250**1W-F71900$$1W-F0070X" };              
                                                                                                                                                                   
var result = (from string d in documents                                                                                                                           
              let docArray = Regex.Split(d, @"\$,\$")                                                                                                                    
              let value = docArray.Select(v => v.Split(new[] { "**"}, StringSplitOptions.RemoveEmptyEntries))                                                            
              select value).ToList();             

Open in new window

The results of this code is.Resulta
0
 

Author Comment

by:Member_2_7967608
ID: 41789725
The issue here is one row can be an array split by $,$ as below

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

the second row can be as below with no $,$  

1**99**New Client**2016-11-02**250**10-F0040Z$$10-F0040Z$$10-F00141
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41789903
Yes, take a close look at the results I posted you will see each input line gets split into two records for each line.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:Member_2_7967608
ID: 41789921
There is case where a row does not have '$,$'. Will it query this record.

1**99**New Client**2016-11-02**250**10-F0040Z$$10-F0040Z$$10-F00141
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41789995
A single record in one string worked for me, I used this as input.
string[] documents = {"1**10**New Client**2016-11-02**250**10-F0040Z$$10-F0040Z$$10-F00141" };

Open in new window

0
 

Author Comment

by:Member_2_7967608
ID: 41790037
Thanks Fernando.
0
 

Author Comment

by:Member_2_7967608
ID: 41790064
Thanks Fernando, One thing I needed was  to eliminate duplicates in the output list req.salesOrders  for  salesOrder.productCode

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}
}

The DTOs of this question are in the link below.  I was not sure if we can implement in the same link.

https://www.experts-exchange.com/questions/28958730/Refactor-C-code.html
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41790116
Hi;

A couple of things. First EE likes only to have one question per thread with followup questions relating to the original question posted. Because your last post does not relate to the original question please open a new question for it and I will be happy to look at it. Secondly what do you mean by duplicates? For example in req.salesOrders you have five fields, do ALL fields of a record have to be exactly the same or only some or one fields be the same for it to be a duplicate?
0
 

Author Comment

by:Member_2_7967608
ID: 41790187
Sorry about it , I was not aware about it.  I put it since it was very much related. I can open new question if u want to.

In the output  list req.saleorders we have remove duplicates by productCode.
In the below example it is value[1] 10, 1W.

req.salesorders should be unique on the value[1] field

> 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

Thanks in advance
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41790193
Yes, please open a new question. Thank you.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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