Help with searching partial string using VB.NET

Hi,

I'm using the code to search by first three characters, how do I modify it to seach for multiple values? For example would like to filtering records for xxx would like to retreive records where xxx is included (xxx;yyy;zzz), similarily if I'm searching for yyy, the record should be found even if xxx and zzz are included.
Dim nodesf1 = (From node In xDocF1.Descendants("Table1")
               Group By Key = New GroupKey With { .ID = node.Element("ID").Value, .CTRY = node.Element("CTRY").Value, .SN = node.Element("SN").Value.SubString(0, 3)} Into grouping = Group 
               Select grouping).ToList()

Open in new window

vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
instead of
.SubString(0, 3)
how about
.Contains("xxx")
0
vcharlesAuthor Commented:
Unfortunately I can't hard code the value "xxx" because it will vary records. Need to use the value of the fields rather than the text.
Thanks.
0
AndyAinscowFreelance programmer / ConsultantCommented:
Hmmm.
Does this work?
replace
.SN = node.Element("SN").Value.SubString(0, 3)
with
node.Element("SN").Value.Contains(.SN)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Fernando SotoRetiredCommented:
Hi Victor;

Not sure what you are asking. The query you posted returns multiple groups of records. This part of the query,
.SN = node.Element("SN").Value.SubString(0, 3)

Open in new window

helps to group the records into groups. Your xxx, yyy, zzz will come out just in different groups.
0
vcharlesAuthor Commented:
HI,

Below is the complete code, the problem is NSN in both files have to be identical, I am running into a situation where I have multiple values in the NSN field(i.e. xxx;yyy;zzz) instead of one value (i.e. xxx) and the code doesn't work even if value in NSN column in file2 is included in file1 because NSN in File1 only contains one value (i.e. xxx)

        Try
            zz = 1
            Dim xDocF1 As XDocument = XDocument.Load(Application.StartupPath + "\File1.xml")
            Dim xDocF2 As XDocument = XDocument.Load(Application.StartupPath + "\File2.xml")


            Dim nodesf1 = (From node In xDocF1.Descendants("AOP40")
                           Group By Key = New GroupKey With {.SN = node.Element("NSN").Value} Into grouping = Group
                           Select grouping).ToList()

            Dim nodesf2 = (From node In xDocF2.Descendants("Table1")
                           Group By Key = New GroupKey With {.SN = node.Element("NSN").Value} Into grouping = Group
                           Select grouping).ToList()

            Dim matchf1f2 = (From group1 In nodesf1
                             Join group2 In nodesf2 On New GroupKey With {.SN = group1(0).Element("NSN").Value} Equals New GroupKey With {.SN = group2(0).Element("NSN").Value}
                             Where group2 IsNot Nothing
                             Select group1, group2).ToList()
            'MsgBox("SSSS")


            For Each grouping In matchf1f2

                Dim g1 As XElement = CType(grouping.group1(0), XElement)
                Dim g2 As XElement = CType(grouping.group2(0), XElement)
                 For Each node As XElement In g2.Descendants()
                    If node.Name.ToString().Equals("NSN") Then
                                        Continue For
                    End If

                    Dim g1Element As XElement = g1.Element(node.Name.ToString())
                    If g1Element IsNot Nothing Then
                        g1Element.Value = node.Value
                    Else
                        g1.Add(node)
                    End If
                Next
            Next
            xDocF1.Save(Application.StartupPath + "\AOP40.xml")
            Label13.Visible = False
        Catch
            Label13.Visible = False
        End Try

Open in new window

0
vcharlesAuthor Commented:
HI again,

I think this part or the code should be modified wit the word "contains" but don't know the right syntax.

Victor
0
AndyAinscowFreelance programmer / ConsultantCommented:
What error message do you get when you try to use 'Contains' ?  Put another way does using Contains solve your original problem?  If it does then please close the question and ask a new one with your new problem.  (EE want a collection of question/answers)

In your posted code I don't actually see any 'Contains'
0
Fernando SotoRetiredCommented:
Hi Victor;

Can you post samples of the File1 and File2 XML documents which shows the issue you are having, without it, it will be difficult to understand the problem you are having.
0
vcharlesAuthor Commented:
Hi,

I'm trying to upload data from file2 to file1 based on matching NSN

For example, if file1 contains:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <MyDataTable>
    <ID>1</ID>
    <NSN>1111-11-111-1111</NSN>
  </MyDataTable>
 <MyDataTable>
    <ID>2</ID>
    <NSN>2222-22-222-2222</NSN>
  </MyDataTable>
 <MyDataTable>
    <ID>3</ID>
    <NSN>3333-33-333-3333</NSN>
  </MyDataTable>
</DocumentElement>

and fil2 contains:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <MyDataTable>
    <ID>1</ID>
    <NSN>1111-11-111-1111</NSN>
  <NSC>AAAA</NSC>
  </MyDataTable>
 <MyDataTable>
    <ID>2</ID>
    <NSN>2222-22-222-2222,9999-99-999-9999;8888-88-777-8888</NSN>
   <NSC>BBBB</NSC>
  </MyDataTable>
 <MyDataTable>
    <ID>3</ID>
    <NSN>6666-66-666-6666;3333-33-333-3333</NSN>
    <NSC>CCCC</NSC>
  </MyDataTable>
</DocumentElement>

File3 should contain the following data:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <MyDataTable>
    <ID>1</ID>
    <NSN>1111-11-111-1111</NSN>
    <NSC>AAAA</NSC>
  </MyDataTable>
 <MyDataTable>
    <ID>2</ID>
    <NSN>2222-22-222-2222</NSN>
    <NSC>BBBB</NSC>
  </MyDataTable>
 <MyDataTable>
    <ID>3</ID>
    <NSN>3333-33-333-3333</NSN>
  <NSC>CCCC</NSC>
  </MyDataTable>
</DocumentElement>

The actual files contain more dataelements.

The problem is unless data from NSNs of both files are identical NSC will not cop to file1.


Thanks,

Victor
0
Fernando SotoRetiredCommented:
Hi Victor;

Try this, it gave me the result you posted. The code below is the only code I modified.

Dim matchf1f2 = (From group1 In nodesf1
                 From group2 In nodesf2
                 Let nsn1 = group1(0).Element("NSN").Value
                 Let nsn2 = group2(0).Element("NSN").Value
                 Let nsn1Ensn2 = If(nsn2.Split(",").Contains(nsn1), nsn1, String.Empty)
                 Where nsn1 = nsn1Ensn2 
                 Where group2 IsNot Nothing
                 Select group1, group2).ToList()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank you for the solution, will try it and get back to you.
0
vcharlesAuthor Commented:
Hi,

It works.

Thank You.
0
Fernando SotoRetiredCommented:
Not a problem Victor, glad it worked for you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.