Accessing Data with C#

A quick guide intended for C# developers who want to learn how to perform CRUD operations on C1's data system using C# and LINQ.

How to Query Data Using LINQ

To query data in the C1 data store, you should use the Get method of DataConnection specifying an existing data type. The method returns an IQueryable instance of the IData interface.

public IQueryable<TData> Get<TData>()
where TData : class, IData

Listing 14: The DataConnection.Get method

You can use the instance to further query it by using LINQ.

To query data in the C1 data store:

  1. Connect to the data system.
  2. Get the IQueryable instance of the data type you need.
  3. Query data by using LINQ.
using (DataConnection connection = new DataConnection())
{
   var myUsers = 
      from d in connection.Get<Demo.Users>()
      where d.Name == "John Doe"
      select d;
}

Listing 15: Querying data using LINQ

Page folder data and page meta data also have the PageId property. Use this property to get folder data or meta data for a specific page. To filter data by the current page's ID, use SitemapNavigator.CurrentPageId.

The code will be the same for both page folder data and page meta data:

using (DataConnection connection = new DataConnection())
{
   var myFolderData = 
      from d in connection.Get<My.Folder.Type>()
      where d.PageId == SitemapNavigator.CurrentPageId
      select d;
}

Listing 16: Querying page folder data

using (DataConnection connection = new DataConnection())
{
   var myMetaData = 
      from d in connection.Get<My.Meta.Type>()
      where d.PageId == SitemapNavigator.CurrentPageId
      select d;
}

Listing 17: Querying page meta data

For information on SitemapNavigator, please see http://api.composite.net/html/P_Composite_Data_DataConnection_SitemapNavigator.htm