using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Services;
using Telerik.Web.UI;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public IList<Product> GetProducts(RadTreeNodeData node) {
		NorthwindDataContext db = new NorthwindDataContext();
		var products = from p in db.Products 
					   where p.Category.CategoryName == node.Text 
					   select p ;
		return products.ToList();
    }

	NorthwindDataContext context = new NorthwindDataContext();

	[WebMethod(EnableSession = true)]
	public List<Product> GetData(int startIndex, int maximumRows, string sortExpression, string filterExpression)
	{
		IQueryable<Product> queryable = context.Products.AsQueryable<Product>();

		if (!String.IsNullOrEmpty(filterExpression))
		{
			queryable = queryable.Where(filterExpression);
		}

		if (!String.IsNullOrEmpty(sortExpression))
		{
			queryable = queryable.OrderBy(sortExpression);
		}

		HttpContext.Current.Session["TotalCount"] = queryable.Count();

		return queryable.Skip(startIndex).Take(maximumRows).ToList();
	}

	[WebMethod(EnableSession = true)]
	public int GetCount()
	{
		return (int)HttpContext.Current.Session["TotalCount"];
	}
}

