Link to home
Start Free TrialLog in
Avatar of sidwelle
sidwelleFlag for United States of America

asked on

Diff of 2 tables using vb.net Linq query

I am using an  XML file to keep track of files moved, no I want a function to return the files not in both table that match a file pattern.  
Both xmaster and xsublist return elements with sub elements, but I don't know how to write a query to return the files not in both tables.

Any help is appreciated.

 
Public Function FindDiffOfList(ByVal MasterList As String, ByVal SubList As String, ByVal FilePattern As String) As String

        Private XmlDoc As Xml.XmlDocument
        Private sDefXmlPath As String = "C:\...\files.xml"

        XmlDoc.Load(sXmlPath)

        Dim xElmDoc As XElement = XElement.Parse(XmlDoc.ToString)
        Dim xMaster As XElement = xElmDoc.Element(MasterList)
        Dim xSubList As XElement = xElmDoc.Element(SubList)

        Dim Diffs = _
            From nm1 In xMaster.Elements("file") _
                From nm2 In xSubList.Elements("file") _
                Where nm1.Element("name") <> nm2.Element("name") and nm1.Element("name") like FilePattern
            Select nmx

         'Return the filenames as a delimited string ...


    End Function

Open in new window

Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi sidwelle;

Can you post samples of all input files so that the schema of the data can be determined to see if you can use set operators.
Avatar of sidwelle

ASKER

Attached is an XML that I am testing with.

The code from the post would called like:

dim sList as string = FindDiffOfList("RemoteList", "LocalList", "*.txt")

MsgBox   sList
files.xml
What would the input variables MasterList and SubList contain for each? Also sXmlPath gets loaded into the variable XmlDoc but what does that file look like?
Its attached, let me know if you can't see it.

Thanks
files.xml.txt
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, let me test and get back to you.
May be Tomorrow before I can get a good test.

Thank You.
I get an error 'XPathSelectElements' is not a member of root.
This machine has a copy of VS9, is that too old ?

What version should I have running ?
from my reading, I thought XElement was the preferred object to use because it included the Linq Lib ?
Sorry but you need to add this import statement at the top of the file.
Imports System.Xml.XPath

Open in new window

Works, but I have few more questions:
1.   Why didn't you use the 'Like' Operator instead of 'EndsWith' ?   Does it consume more resources ?
2.   I expected the results to be more like a recordset that contained Name, date, size ...
       Is this too difficult ?
3    I expected the code to look more like standard SQL, can you do that with XML /  Linq ?

Thanks
(1) : The Linq to XML provider does not support a Like operator.

(2): In your question you state, "I want a function to return the files not in both table that match a file pattern.", your sample does not show anything else as well. When posting a question you need to be specific enough so that a solution has what you want. To this part of (2), "Is this too difficult ?", not much more difficult but a lot more work.

(3) You only can do what the Linq to XML provider has provided, remember that Linq is a SQL like language and NOT the same language.
1.): I swapped it in a it worked (Where (r.Value Like FilePattern) ) ?!
2.): Guess I didn't, just thought the nature of result would contain it.
3.): Can you suggest any good resources that would provide some working examples ? (Besides M.S.)

I always appreciate it when you answer my questions.

Thank You.
Not a problem sidwelle, glad to help.