title: Sitecore7 Linq to Sitecore only works with SearchResultItem but not with Custom Mapped Class
body: <p>I have this very weird problem that I cannot get my head around. Perhaps someone could point out what I am doing wrong.</p>
  
  <p>Basically, I am just trying to search items using Linq to Sitecore.</p>
  
  <p>So, my classes look like ( I am using glass too)</p>
  
  <pre><code>[SitecoreType(TemplateId = "{TEMPLATE_GIUD}")]
  public class MyMappedClass : SharedFieldClass
  {
      [SitecoreField(FieldName = "mylist")]
      public virtual IEnumerable&lt;SharedFieldClass&gt; MyMultilistField { get; set; }
  
  
      [SitecoreField(FieldName = "field1")]
      [IndexField("field1")]
      public virtual MyKeyValue field1 { get; set; }    
  }
  [SitecoreType]
  public class MyKeyValue
  {
      public virtual Sitecore.Data.ID Id {get;set;}    
      public virtual string MyValue{get;set;}
  }
  </code></pre>
  
  <p>So when I do the below query it works as it's supposed to.</p>
  
  <pre><code>    var results = context.GetQueryable&lt;SearchResultItem&gt;()
                    .Where(c =&gt; ((string)c["field1"]) == "{GUID}").GetResults();
  </code></pre>
  
  <p>But, when I do the below it returns 0 result.</p>
  
  <pre><code>List&lt;MyMappedClass&gt; results = context.GetQueryable&lt;MyMappedClass&gt;()
                                .Where(c =&gt; c.field1.MyValue == "{GUID}").ToList();
  </code></pre>
  
  <p>I have read <a href="http://kamsar.net/index.php/2013/04/sitecore-7-linq-gotchas/" rel="nofollow">this link</a> . And I have followed the 2nd process described <a href="http://glass.lu/docs/tutorial/sitecore/tutorial25/tutorial25.html" rel="nofollow">here</a> for Glass to work with Sitecore7 Search (the "SharedFieldClass" contains all the basic index fields).</p>
  
  <p>This is a pretty obvious scenario and I'm sure lots of people have done it already and I am doing something silly here.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>/## EDIT ##/</strong></p>
  
  <p>Okay so I've done a bit more digging on this. Not sure if it's a bug in ContentSearch/Luncene.NET API or I am missing something BUT seems like what was posted <a href="http://kamsar.net/index.php/2013/04/sitecore-7-linq-gotchas/" rel="nofollow">here</a> is probably not TRUE and if you have a complex field type ( which you will ) you can not query with a mapped class against Lucene. ( not sure if this is also the case for Solr). If you are doing search against simple type like string and int then it works like a charm.</p>
  
  <p>SO here're my findings:</p>
  
  <ol>
  <li>After enabling DEBUG and LOG on for contentsearch I found that if I query like this <code>context.GetQueryable&lt;MyMappedClass&gt;().Where(c =&gt; c.field1.MyValue == "{GUID}")</code> what it gets translated into is <code>DEBUG Executing lucene query: field1.value:7e9ed2ae07194d83872f9836715eca8e</code> and as there's no such thing in the index named "field1.value" the query doesn't return anything. The name of the index is actually just "field1". <strong>Is this a bug ??</strong></li>
  <li>However, query like this <code>context.GetQueryable&lt;SearchResultItem&gt;()                   .Where(c =&gt; ((string)c["field1"]) == "{GUID}").GetResults();</code> works because it gets translated into <code>"DEBUG Executing lucene query: +field1:7e9ed2ae07194d83872f9836715eca8e"</code>.</li>
  <li><p>I also did write a method in my mapped class like below:</p>
  
  <pre><code>public string this[string key]
  {
      get
      {
          return key.ToLowerInvariant();
      }
      set { }
  }
  </code></pre></li>
  </ol>
  
  <p>Which allowed me write the below query with my MyMappedClass.</p>
  
  <pre><code>results2 = context.GetQueryable&lt;MyMappedClass&gt;().Where(c =&gt; c["filed1"]== "{GUID}").ToList();
  </code></pre>
  
  <p>This returned expected result. <strong>BUT</strong> the values of the fields in MyMappedClass are not filled ( in fact they're all null except the core/shared values like templateid, url etc which are populated in the filled document). So the result list are pretty much useless. I could do a loop over all of them and manually get the values populated as I have the itemid. But imagine the cost for a large result set.</p>
  
  <p><strong>Lastly I did this:</strong></p>
  
  <pre><code>&lt;fieldType fieldTypeName="droplink"                           storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String"   settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider" /&gt;
  </code></pre>
  
  <p>So this returned populated "field1" with the itemid in lucene query using "IndexViewer2.0". <strong>BUT</strong> this fails for MyMappedClass too as the value of "field1" in the document is a System.string .. but it is mapped as "MyKeyValue" in MyMappedClass <strong>SO</strong> it throws the below exception </p>
  
  <pre><code>Exception: System.InvalidCastException
  Message: Invalid cast from 'System.String' to 'MyLib.MyKeyValue'.
  </code></pre>
  
  <p>SO, the big question is:
  <strong>How does one query using his/her mapped class using the cool ContentSearch API ?</strong></p>