C# Dynamically apply evaluation rule on a collection
<p>I have a fixed-length data file need to persistence into database. I use a XML file to define the length for the fields and use a list of FieldItem class to store the data.</p>

<pre><code>   class FieldItem
   {
        public string ObjectName {get; set;}
        public string ObjectProperty {get; set;}
        public string ObjectValue {get; set;}

        public FieldItem()
       {
       }
   }
</code></pre>

<p>So the FieldItem will look like</p>

<pre><code>var fieldItem = new FieldItem
                {
                   ObjectName = "Company",
                   ObjectProperty = "Name",
                   ObjectValue = "ABC Corp."
                }
</code></pre>

<p>After get the list of FieldItem, I will do refection to create Company and other domain objects to save them into database. </p>

<p>But before they are saved into database, I have some business rules needed to be applied to validate the data line. My data line will look like:</p>

<pre><code>var fieldItemList = new List&lt;FieldItem&gt;(){
                new FieldItem {
                 ObjectName="Company",
                 ObjectProperty = "Name",
                 ObjectValue = "ABC"
                }

                new FieldItem{
                 ObjectName = "Product",
                 ObjectProperty = "ProductCode",
                 ObjectValue ="XYZ0123"

                    new FieldItem{
                 ObjectName = "Product",
                 ObjectProperty = "ProductName",
                 ObjectValue ="Christmas Tree"  
                }

                    // other FieldItem objects...

            }
</code></pre>

<p>For example, my rule is to check if the company == "ABC" and ProductCode == "XYZ0123". Those rules are created by users and stored as a string in the database. What I am doing right now is to use Microsoft's System.Linq.Dynamic to evaluate the rule, such as </p>

<pre><code>string validationRule = " (ObjectName == \"Company\" And ObjectProperty=\"CompanyName\" And ObjectValue = \"ABC Corp.\") And (ObjectName == \"Product\" And ObjectProperty=\"ProductCode\" And ObjectValue = \"XYZ0123\") ";

  var query = fieldItemList.AsQuerable().Where(validationRule);
</code></pre>

<p>then check if it has one row return to tell if that data row has passed the rule or not.
Obviously, it is too verbose. Do you have any better suggestion? What should I do if I only like my rule expression like: "Company.CompanyName = 'ABC Corp.' and Product.ProductCode = 'XYZ001'"?</p>
