LibrarySites.Banner

Understanding Drag Text for LINQPad Driver for Sitecore

LINQPad Driver for Sitecore 1.8 introduced a smarter "drag text" feature. This is the feature in LINQPad that allows you to drag an item under a connection into the query editor. The result is a LINQ expression being added. This post describes the LINQ expression that is added and how that expression is generated.

The following is the default "drag text":

01.var index = ContentSearchManager.GetIndex("sitecore_master_index");
02.using (var context = index.CreateSearchContext())
03.{
04.  context.GetQueryable<SearchResultItem>()
05.    .Where(item => item.Name == "Home")
06.    .Where(item => item.Language == "en")
07.    .Select(item => new {Name = item.Name, Id = item.ItemId.ToString()} )
08.    .Dump();
09.}

This expression means:

  • (line 1) Use the index named sitecore_master_index
  • (line 4) Use the type SearchResultItem to represent a search result
  • (lines 5 and 6) Limit the search to results with a name of "Home" and a language of "en"
  • (line 7) Read the value of the Name field and the ItemId field
  • (line 8) Display the results in LINQPad

The Sitecore search provider knows how to build this query based on the settings on the SearchResultItem type. The property Name maps to the field in the search engine _name. This is configured through the attribute Sitecore.ContentSearch.IndexFieldAttribute.

In addition:

  • The property Language maps to the field in the search engine _language.
  • The property ItemId maps to the field in the search engine _group.

Using a different search result item type

The type for the IQueryable<T> object that is used is determined by the "search result item type" that is specified on the Sitecore connection.


But what happens to the "drag text" when you change the type specified for the IQueryable<T> object? The LINQPad for Sitecore driver looks at the type you've selected and made the following decisions:

  • If the type has a property that is mapped to the field in the search engine _name, include a Where expression using that property (line 5 in the default expression).
  • If the type has a property that is mapped to the field in the search engine _language, include a Where expression using that property (line 6 in the default expression).
  • If the type has a property that is mapped to the field in the search engine _name, include that property in the Select expression (line 7)
  • If the type has a property that is mapped to the field in the search engine _group, include that property in the Select expression (line 7)

Example 1

The following POCO...

namespace Testing.Linqpad
{
  public class NameOnlyPoco
  {
    public string Name { get; set; }
    [IndexField("_name")]
    public string MyName { get; set; }
  }
}

...results in the following "drag text":

var index = ContentSearchManager.GetIndex("sitecore_master_index");
using (var context = index.CreateSearchContext())
{
  context.GetQueryable<Testing.Linqpad.NameOnlyPoco>()
    .Where(item => item.MyName == "Home")
        .Select(item => new {Name = item.MyName} )
          .Dump();
}

Example 2

The following POCO...

namespace Testing.Linqpad
{
  public class LanguageOnlyPoco
  {
    [IndexField("_language")]
    public string MyLang { get; set; }
  }
}

...results in the following "drag text":

var index = ContentSearchManager.GetIndex("sitecore_master_index");
using (var context = index.CreateSearchContext())
{
  context.GetQueryable<Testing.Linqpad.LanguageOnlyPoco>()
    .Where(item => item.MyLang == "en")
        .Select(item => new {} )
          .Dump();
}

Example 3

The following POCO...

namespace Testing.Linqpad
{
  public class ItemIdOnlyPoco
  {
    [IndexField("_group")]
    public string MyGroup { get; set; }
  }
}

...results in the following "drag text":

var index = ContentSearchManager.GetIndex("sitecore_master_index");
using (var context = index.CreateSearchContext())
{
  context.GetQueryable<Testing.Linqpad.ItemIdOnlyPoco>()
    .Select(item => new {Id = item.MyGroup.ToString()} )
      .Dump();
}

Example 4

The following POCO...

namespace Testing.Linqpad
{
  public class CompletePoco
  {
    public string Name { get; set; }
    [IndexField("_name")]
    public string MyName { get; set; }
    [IndexField("_language")]
    public string MyLang { get; set; }
    [IndexField("_group")]
    public string MyGroup { get; set; }
  }
}

...results in the following "drag text":

var index = ContentSearchManager.GetIndex("sitecore_master_index");
using (var context = index.CreateSearchContext())
{
  context.GetQueryable<Testing.Linqpad.CompletePoco>()
    .Where(item => item.MyName == "Home")
    .Where(item => item.MyLang == "en")
      .Select(item => new {Name = item.MyName, Id = item.MyGroup.ToString()} )
        .Dump();
}

Conclusion

I hope you understand how the "drag text" is generated and why you're seeing what you see in LINQPad when you use it.

And, by the way, if you want to change the "drag text" that is generated, you can extend the default schema builder and override the method GetDragText. I covered how to create custom components for the LINQPad Driver for Sitecore in my previous post.

  • Hi Adam I was trying install and run the Linqpad plugin but i am getting error when i ran one of sample above. Can you help me with that.  Unable to open configSource file 'App_Config\Log4NetSitecore.config'.