title: Using Linq to Entities to find the best match in table
body: <p>I have a table with two columns that will be used as filtering criteria. Using Linq to Entities, I want to pass in two values and always return the closest match. For example, (1,2) should return (1,2) not (null,2) as the closest. If neither value finds a match, then the Null, Null column match is used.</p>
  
  <p>Below is the sample code.</p>
  
  <p>If 3,3 is passed in the first Data (null,null) should be returned since there are no matches.</p>
  
  <p>If 1,3 is passed in , the second Data (1,null) should be returned.</p>
  
  <p>If 3,2 is passed in , the third Data (null,2) should be returned.</p>
  
  <p>If 1,2 is passed in, the fourth Data(1,2) should be returned.</p>
  
  <p>Is this possible to do in one Linq query since it is generating a database hit?</p>
  
  <pre><code>public class Data
          {
              public string prop1 { get; set; }
              public  string prop2 { get; set; }
          }
  
          static void Main(string[] args)
          {
              List&lt;Data&gt; datas = new List&lt;Data&gt;
                                     {
                                         new Data
                                             {
                                                 prop1 = null,
                                                 prop2 = null
                                             },
                                         new Data
                                             {
                                                 prop1 = "1",
                                                 prop2 = null
                                             },
                                         new Data
                                             {
                                                 prop1 = null,
                                                 prop2 = "2"
                                             },
                                         new Data
                                             {
                                                 prop1 = "1",
                                                 prop2 = "2"
                                             }
                                     };
  
  
          }
  </code></pre>