What if, depending on User's selection I would have to add a Where statement? For example Where FileName Like '%blue%'
Or more than one Where Statement, for example: Where FileName Like '%blue%' And FileExtension = '.doc'
How can I pass in such parameters and dynamically create such statement, no matter how many parameters?

Same thing with Order by - this time a User wants to Order By FileName Asc, and the other time by FolderAncestors Desc, FileExtension Asc.

In your original XML file you do not have a "Ext" Attribute in the File node. Have you changed that so that File nodes do have Ext Attribute? If so can you please post new XML document so I can test the query.

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

To be able to do what you want we need to add a class which matches the values being returned by the select statement, in the below code I call this class XMLFileData. We also need to build the query in stages only adding the Where clause's when needed.

Dim xdoc As XDocument = XDocument.Load("...\db.xml")' Variable to add Where clause's to the base queryDim searchFileType = "image"Dim searchFileName = "txt"' Starts the process of building the queryDim listOfFilesBase = xdoc.Descendants("File")' If we need to search for a file name then this Where clause gets addedIf Not String.IsNullOrEmpty(searchFileName) Then listOfFilesBase = listOfFilesBase.Where(Function(fn) fn.Attribute("FileName").Value.Contains(searchFileName))End If' If we need to search for a file type then this Where clause gets addedIf Not String.IsNullOrEmpty(searchFileType) Then listOfFilesBase = listOfFilesBase.Where(Function(fe) fe.Attribute("FileType").Value.Contains(searchFileType))End If' Now that all the Where clause's have been added complete by adding the select clauseDim listOfFiles = listOfFilesBase.Select(Function(d) New XMLFileData With _{ _ .FileType = d.Attribute("FileType"), _ .FileName = d.Attribute("FileName"), _ .FileSize = d.Attribute("Size"), _ .FolderAncestors = d.Ancestors("Folder").Aggregate("", Function(path As String, current As XElement) current.Attribute("FolderName").Value & "\" & path) _}).ToList()' Class to build the result setPublic Class XMLFileData Public FileType As XAttribute Public FileName As XAttribute Public FileSize As XAttribute Public FolderAncestors As StringEnd Class

To your question, "And is there a sign like in SQL you have a % sign, so I can query something like 'mico%sf' where % is being whatever is in the middle", Well you do not have a sign but the Contains method translate to a SQL %. This can match in the beginning, middle or end. You can also use the methods StartsWith and EndsWith to match the beginning and the end of a string.

Featured Post

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database