Linq Filter Sort

Linq Filter Sort

LINQ

  • A set of extensions to C# and VB development languages
  • Allows developers to manipulate in-memory data similar to how a database query is written
  • Contains about 40 query operators

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • System.Linq

Linq Filter Sort C# Web Control Code

Sitecore.Data.Items.Item context = this.GetItem();
    if (context == null) return;

    Sitecore.Data.Items.Item developers = Sitecore.Context.Database.GetItem("/sitecore/content/Home/Developers");
    if (developers == null) return;

    var items = developers.Axes.GetDescendants();
    var codeSnips = from item in items
    where item.TemplateName == "TIC-CodeSnippet"
    orderby item.Name ascending
    select item;

    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
    output.Write("C# Web Control Real-time Output");
    output.RenderEndTag();

    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
    output.Write("{0}: {1}", "Item", context.DisplayName);
    output.RenderEndTag(); //P
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
    output.Write("{0}", "Displaying First Ten Items");
    output.RenderEndTag(); //P

    int i = 0;
    foreach (Sitecore.Data.Items.Item item in codeSnips)
    {
 i++;
 output.Write("{0}", item.DisplayName + "<br/>");
 if (i > 10) break;

    }
    output.RenderEndTag(); //Div


Linq Filter Sort C# Web Control Output

Displaying First Ten Items

Adding Fields
Adding Items
Adding Links
Adding Sections
Adding Versions
Administrator Logins
Analytics Session
Analytics Tracker
Archived Items
Assigning Media Files
Assigning Properties

Sources