Project DescriptionLinqToolkit is a lightweight class library.
It is designed to help create custom Linq providers.
Class
Query based on
IQueryable Tales - LINQ to LDAP from
Bart De Smet's online blog
http://community.bartdesmet.net/blogs/bart/archive/2007/04/05/the-iqueryable-tales-linq-to-ldap-part-0.aspxPlease take a look on LinqToolkit.Test project for usage examples.
Description:To create custom provider you need to implement several interfaces and inherit from
Query class.
InterfacesIQueryContext provides for
Query object:
- Factory methods to create operations for Where operator:
- JoinOperation ( AndAlso, OrElse )
- UnaryOperation ( Not, MemberAccess )
- BinaryOperation ( Equal, NotEqual, Multiply, Add and so on )
- CallOperation ( Contains, StartWith and so on )
- Factory methods to build custom operators:
- Without parameters
- With property parameter
- With constant value parameter
IQueryOptions provides for
Query object:
- Filter expression property for Where operator
- Array of properties to read from data source
IBaseOperation and
IJoinOperation interfaces help to build Where operator.
Query class example:
public class CustomQuery<TEntity>: Query<CustomQueryContext, TEntity> {
protected override Query<QueryContext, T> Copy<T>() {
return new CustomQuery<T>();
}
protected override IEnumerable<object> ExecuteRequest() {
var options = this.Context.Options;
....
return result;
}
}
AttributesSourceAttributeGives a custom name for data source. By default data source has a name of TEntity class. This data source name is stored in IQueryOptions.Source property.
SourcePropertyAttributeGives a custom name for a data source property. By default property names are the same as TEntity property and field names. Data source property names are stored in IQueryOptions.PropertiesToRead property.
IgnoreAttributeSays to
Query class to ignore properties or fields for which this attribute is assigned.
SimpleQuery namespaceUse SimpleQuery.QueryContext for simple providers:It contains implementation for
IQueryContext and
IQueryOptions interfaces.
Using this context you do not need to create custom query context and options, but only inherit from
Query class.
Property Options gives an access to:
- Filter - parsed WHERE operator
- PropertiesToRead - property names based on SELECT operator
- Operators - other operators applied to query (Distinct, Take etc.)
Use Query.Where( BaseOperation ) to build conditions dynamically:Example:
var filter =
new JoinOperation( ExpressionType.AndAlso,
new BinaryOperation( ExpressionType.GreaterThan, "PropertyName", 10 ),
new UnaryOperation( ExpressionType.Not, "FieldName" )
);
result =
from item in new SomeQuery()
where filter
select item;
It is possible to apply additional conditions:
result =
from item in new SomeQuery()
where filter
where item.Name.Contains("abc")
select item;
Or:
result =
new SomeQuery()
.Where( filter )
.Where( item => item.Name.Contains("abc") );
Use QueryOptions.Transform for XSL transformation:Using this extension you can transform options e.g. to SQL query.
LinqToolkit\SimpleQuery\QueryOptions.xsd - describes the query options xml scheme.
LinqToolkit.Test\Extensions\Sample.xslt - gives a sample for XSL transformation.
Enjoy,
Michael Borisov.