LINQ to Sense/Net examples

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.3
  • Enterprise
  • Community
  • Planned

Overview

LINQ to Sense/Net

Language-Integrated Query (LINQ) provides query capabilities for many types of data store. Sense/Net ECMS contains an implementation of this concept that provides a way to query against the Sense/Net Content Repository. The LINQ to Sense/Net article provides the overview and basics of the implementation of LINQ in Sense/Net ECMS. This article provides examples and best pratices for developers and portal builders for using LINQ to Sense/Net in Content Views, ContentHandlers or any source code related to Sense/Net.

Details

The following examples demonstrate the power of LINQ to Sense/Net and provide best practices on how to use the feature.

To see a detailed list of the supported operations, see the LINQ to Sense/Net operations article.

Simple field filtering

Strongly typed property

Content.All.Where(c => c.Id == 1234); // content by identity
Content.All.Where(c => c.ModificationDate > DateTime.Now.AddDays(-1)); // content from the last 24 hours
Content.All.Where(c => c.DisplayName.StartsWith("{prefix}")); // content by display name prefix

Content Field indexer

Content.All.Where(c => (int)c["Id"] == 1234);
Content.All.Where(c => (string)c["Model"] == "Model 1");

Content handler queries

Content.All.Where(c => ((Site)c.ContentHandler).EnableClientBasedCulture == true); // casting
Content.All.OfType<Site>().Where(c => c.EnableClientBasedCulture == true)); // type filter using OfType

Reference filtering

Content.All.OfType<Article>().Where(c => c.Author == author); // filter by existence for a single reference field
Content.All.OfType<Article>().Where(c => c.Related.Contains(refArticle)); // filter by existence for a multi reference field
Content.All.OfType<Article>().Where(c => c.Author == null); // empty reference

Type filtering

Search for exact type:

Content.All.Where(c => c.ContentType.Name == "Group");
Content.All.Where(c => c.ContentType == ContentType.GetByName("Group"));
Content.All.Where(c => c.Type("Car"));

Search for derived types too:

Content.All.Where(c => c.ContentHandler is Group);
Content.All.Where(c => typeof(Group).IsAssignableFrom(c.ContentHandler.GetType()));
Content.All.Where(c => c.TypeIs("Car"));

Ordering and Partitioning

// content from last month, 9th page
Content.All.Where(c => c.ModificationDate > DateTime.Now.AddMonths(-1)).OrderByDescending(c => c.ModificationDate).Skip(80).Take(10);

Element operators

Content.All.Where(c.Id == 1234).First(); // Load content that must exist.
Content.All.First(c.Id == 1234); // Load content that must exist.
Content.All.Where(c.Id == 1234).FirstOrDefault(); // Load content that may exist.
Content.All.FirstOrDefault(c.Id == 1234); // Load content that may exist.

Quantifier

Content.All.Any(c => c.InTree("/Root/.../MyWorkspace") && c.ContentHandler is MyContentType));

Aggregation

Content.All.Where(c => c.InTree("/Root/.../MyWorkspace") && c.ContentHandler is MyContentType)).Count();
Content.All.Count(c => c.InTree("/Root/.../MyWorkspace") && c.ContentHandler is MyContentType));

Projection

Please note that we use the AsEnumerable method here as described in the LINQ to Sense/Net operations article.

var items = Content.All.Where(c => c.Id < 8).AsEnumerable().Select(c => new { Id = c.Id, FriendlyName = c.DisplayName });
foreach (var item in items)
	Console.WriteLine("Id: {0}, Name: {1}", item.Id, item.FriendlyName);

Grouping

var groups = Content.Load("/Root/IMS/BuiltIn/Portal").Children.OrderBy(c => c.Name).AsEnumerable().GroupBy(c => c.Name.First());
foreach (var group in groups)
{
	Console.WriteLine("{0}:", group.Key).AppendLine();
	foreach (var item in group)
		Console.WriteLine(item.Name);
}

The result of the code above is the following:

A:
Administrator
Administrators
C:
Creators
E:
Everyone
L:
LastModifiers
V:
Visitor

Sense/Net specific LINQ methods

Content from multiple places in one list

Content.All.Where(c => c.InTree("/Root/...path1") 
	|| c.InTree("/Root/...path2") 
	|| c.InFolder("/Root/...path3")).OrderByDescending(c => c.ModificationDate).Take(5);

Related links