vituxa
asked on
Dynamically insert Where and OrderBy clauses in Linq. How?
Hello.
I have this Lambda ExpressionSelect Code that works very well.
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.
Please help with examples.
I have this Lambda ExpressionSelect Code that works very well.
Dim listOfFiles = xdoc.Descendants("File") _
.Select(Function(sfn) New With _
{ _
.FileExtension = sfn.Attribute("Ext"), _
.FileName = sfn.Attribute("FileName"), _
.FileSize = sfn.Attribute("Size"), _
.FolderAncestors = sfn.Ancestors("Folder").Aggregate("", Function(path As String, current As XElement) current.Attribute("FolderName").Value & "\" & path) _
}).ToList()
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.
Please help with examples.
ASKER
Can you please take my example and add a dynamic where and orderby to it?
Hi vituxa;
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.
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.
ASKER
Oh please don't worry about the ext. If you can give me the example of any other attribute - size, FileCreatedOn... Anything really.
Thank you Prodigy.
Thank you Prodigy.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Thank you Prodigy!!! Once again saved me!
Not a problem vituxa, glad to be of help.
http://msdn.microsoft.com/en-us/library/bb882637.aspx