using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using Telerik.Web.UI;

// NOTE: If you change the class name "GridWcfService" here, you must also update the reference to "GridWcfService" in Web.config.
[DataContract]
public class Product
{
    [DataMember]
    public int ProductID { get; set; }
    [DataMember]
    public string ProductName { get; set; }
    [DataMember]
    public decimal? UnitPrice { get; set; }
    [DataMember]
    public short? ReorderLevel { get; set; }
    [DataMember]
    public bool Discontinued { get; set; }
}

public class ResultData
{
    public int Count { get; set; }
    public List<Product> Data { get; set; }
}

[ServiceKnownType(typeof(Product))]
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class GridWcfService  
{
    [OperationContract]
    public ResultData GetDataAndCount(int startRowIndex, int maximumRows, string sortExpression, string filterExpression)
    {
        GridBindingData data = RadGrid.GetBindingData("LinqToSql.NorthwindDataContextDataContext", "Products", startRowIndex, maximumRows, sortExpression, filterExpression);
        ResultData result = new ResultData();
        result.Data = data.Data.OfType<LinqToSql.Product>().Select(p => new Product()
        {
            ProductID = p.ProductID,
            ProductName = p.ProductName,
            UnitPrice = p.UnitPrice,
            ReorderLevel = p.ReorderLevel,
            Discontinued = p.Discontinued
        }).ToList();
        result.Count = data.Count;
        return result;
    }
}    
