How to Query Data Using LINQ
To query data in the Data store, you should use the Get method of DataConnection specifying an existing datatype. The method returns an IQueryable instance of the IData interface.
public IQueryable<TData> Get<TData>() where TData : class, IData
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 datatype 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;
}
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;
}