title: Get value of dictionary element and used in where condition in Entity Framework
body: <p>I have a SQL table (<code>Product</code>) like this: </p>
  
  <pre><code>+----+---------+---------+------+----------------+
  | ID |CompanyID|   Name  | Code |  VisibleFields |
  +----+---------+---------+------+----------------+
  | 1  |   11    |    A    |  C1  |      ,1,2,     |
  +----+---------+---------+------+----------------+
  | 2  |   22    |    B    |  C2  |      ,2,       |
  +----+---------+---------+------+----------------+
  </code></pre>
  
  <p>And a <code>enum</code>:</p>
  
  <pre><code>public enum Enum_ProductField
          {                             
              Name = 1,
              Code = 2                
          }
  </code></pre>
  
  <p>I used this <code>enum</code> in this method:</p>
  
  <pre><code>public override List&lt;Product&gt; GetListProducts(string Name,string Code)
          {                             
              string Name_F = "," + ((int)Product.Enum_ProductField.Name).ToString() + ",";
              string Code_F = "," + ((int)Product.Enum_ProductField.Code).ToString() + ",";
  
              List&lt;Product&gt; retVal = null;
              using (MarketEntities db = new MarketEntities())
              {              
                  retVal = (from P in db.Products
                            where                                
                               (Code == string.Empty || (P.Code == Code &amp;&amp; P.VisibleFields.Contains(Code_F))) &amp;&amp;
                               (Name == string.Empty || (P.Name.Contains(Name) &amp;&amp; P.VisibleFields.Contains(Name_F)))
                            select P).ToList();
              }
              return retVal;
          }
  </code></pre>
  
  <p>If I call <code>GetListProducts("A", string.empty)</code> return product with <code>ID = 1</code>.</p>
  
  <p>If I call <code>GetListProducts("B", string.Empty)</code> return null because <code>VisibleFields</code> of product with <code>ID = 2</code> does not exist <code>,1,</code> and it should not be displayed.</p>
  
  <p>I want to change <code>GetListProducts</code> method and use a <code>Dictionary</code> for <code>enum</code> like this:</p>
  
  <p><strong>1- Create Dictionary in <code>GetProductFieldAsDictionary</code></strong></p>
  
  <pre><code>public static Dictionary&lt;T, string&gt; GetProductFieldAsDictionary&lt;T&gt;() where T : struct, IConvertible
          {
              var visibleFieldsValues = Enum.GetValues(typeof(T)).Cast&lt;T&gt;().ToDictionary(e =&gt; e, e =&gt; "," + ((int)(object)e).ToString() + ",");
              return visibleFieldsValues;
          }
  </code></pre>
  
  <p>This method gets a <code>enum</code>and return all visible fields with <code>Key</code> and <code>Value</code>.    </p>
  
  <p><strong>2- Use <code>GetProductFieldAsDictionary</code> in query</strong></p>
  
  <pre><code>public override List&lt;Product&gt; GetListProducts(string Name, string Code)
          {           
              var _ProductFields = ISTIUtility.GetProductFieldAsDictionary&lt;Product.Enum_ProductField&gt;();            
  
              List&lt;Product&gt; retVal = null;
              using (MarketEntities db = new MarketEntities())
              {              
                  retVal = (from P in db.Products
                            where
                               (Code == string.Empty || (P.Code == Code &amp;&amp; P.VisibleFields.Contains(_ProductFields[Product.Enum_ProductField.Code]))) &amp;&amp;
                               (Name == string.Empty || (P.Name.Contains(Name) &amp;&amp; P.VisibleFields.Contains(_ProductFields[Product.Enum_ProductField.Name])))
                            select P).ToList();
              }
              return retVal;
          }
  </code></pre>
  
  <p>But I get <code>Run-time error</code> because I can't use <code>Dictionary</code> in <code>linq</code> like code above.</p>
  
  <blockquote>
    <p>I do not want to get value of <code>Dictionary</code> out of the <code>linq query</code>.</p>
  </blockquote>
  
  <p>I read a lot of posts, but I did not find a solution for it. Is there any way to do use this?</p>