I have an XML document defined like so
XML File
<TABLE> <RECORD> <PAGENUMBER> 1 Of 1</PAGENUMBER> <OtherFields1>..</OtherFields1> <OtherFields2>..</OtherFields2> </RECORD> <RECORD> <PAGENUMBER> 1 Of 2</PAGENUMBER> <OtherFields1>..</OtherFields1> <OtherFields2>..</OtherFields2> </RECORD> <RECORD> <PAGENUMBER> 2 Of 2</PAGENUMBER> <OtherFields1>..</OtherFields1> <OtherFields2>..</OtherFields2> </RECORD> <RECORD> <PAGENUMBER> 1 Of 1</PAGENUMBER> <OtherFields1>..</OtherFields1> <OtherFields2>..</OtherFields2> </RECORD> </TABLE>
I’m trying to create a dynamic query where i can put a ‘Fieldname’, ‘Operand’, ‘Value’ in a config and LINQ can build a predicate based on it.
Config File
<CLAUSES> <CLAUSE name='PAGENUMBER' operand='!='>Page 1 Of 1</CLAUSE> <CLAUSE name='OtherFields1' operand='!='>String.Empty</CLAUSE> </CLAUSES>
Here is the LINQ i’m using now
LINQ Query
XDocument jobXML = XDocument.Load(JobFile); List<ClauseObj> clauses = new List<ClauseObj>(); clauses.Add(new ClauseObj { Field = 'PAGENUMBER', Operand = '!=', Value = ' Page 1 Of 1' }); var q = jobXML.Descendants('RECORD').AsEnumerable(); foreach (var c in clauses) { switch (c.Operand) { case '!=': q = q.Where(r => r.Element(c.Field).Value != c.Value); break; } }
how can i make it read the where clauses ‘dynamically’ and also specify which field names to return?
1 Answer