How to use?
I want to create a query according to the form filled in by the user.
The query will be run on the web server.
Client
<riac:DomainDataSource x:Name="dsProducts" AutoLoad="True" QueryName="GetProduct" LoadSize="30" >
<riac:DomainDataSource.DomainContext>
<domain:TestDomainContext />
</riac:DomainDataSource.DomainContext>
<riac:DomainDataSource.QueryParameters>
<riac:Parameter ParameterName="filter" />
</riac:DomainDataSource.QueryParameters>
<riac:DomainDataSource.SortDescriptors>
<riac:SortDescriptor PropertyPath="Name" Direction="Ascending" />
</riac:DomainDataSource.SortDescriptors>
</riac:DomainDataSource>
using ex = System.Linq.Expressions;
try {
System.Linq.Expressions.Expression expRoot = null;
ex.Expression<Func<Demo.Web.Product, bool>> cc;
//Build Query
//-- Price --
if(!string.IsNullOrEmpty(txtPrice1.Text)) {
cc = p => p.ListPrice >= decimal.Parse(txtPrice1.Text);
expRoot = expRoot.LogicalAnd(cc);
}
if(!string.IsNullOrEmpty(txtPrice2.Text)) {
cc = p => p.ListPrice <= decimal.Parse(txtPrice2.Text);
expRoot = expRoot.LogicalAnd(cc);
}
//Size
if(!string.IsNullOrEmpty(txtSize1.Text)) {
cc = p => p.Size >= decimal.Parse(txtSize1.Text);
expRoot = expRoot.LogicalAnd(cc);
}
if(!string.IsNullOrEmpty(txtSize2.Text)) {
cc = p => p.Size <= decimal.Parse(txtSize2.Text);
expRoot = expRoot.LogicalAnd(cc);
}
//Colors
string[] bikeColors = new String[] { txtColor1.Text, txtColor2.Text, txtColor3.Text };
ex.Expression expColor = null;
foreach(var item in bikeColors) {
if(!string.IsNullOrEmpty(item)) {
string ss = item.ToUpper();
cc = p => p.Color.ToUpper().Contains(ss);
expColor = expColor.LogicalOr(cc);
}
}
expRoot = expRoot.LogicalAnd(expColor);
byte[] query = null;
if(expRoot != null) {
query = expRoot.Serialize();
}
dsProducts.QueryParameters[0].Value = query;
dsProducts.Load();
} catch {
MessageBox.Show("Invalid Input");
}
Server
namespace Demo.Web
{
using System.Linq;
using System.Linq.Expressions;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using ExpressionSerialization;
[EnableClientAccess()]
public class TestDomainService : DomainService
{
[Query(HasSideEffects = true, IsComposable = true)]
public IQueryable<Product> GetProduct(byte[] filter) //System.Data.Linq.Binary
{
ProductList li = System.Web.HttpContext.Current.Session["products"] as ProductList;
var query = from n in li select n;
IQueryable<Product> qq = query.AsQueryable();
//--------------------------------
if(filter != null) {
qq = ExpressionExtension.ApplyFilter(qq, filter);
}
//--------------------------------
return qq;
}
}
}