Accessing Data with C#

How to Query Data Using LINQ

To query data in the 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 8: The DataConnection.Get method

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

To query data in the 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 9: 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 10: 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 11: Querying page meta data

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

For information about joining data from two data types, please see “Joining Data”.