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;
using System.Globalization;


[DataContract]
public class Employee
{
    [DataMember]
    public int EmployeeID { get; set; }
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public DateTime? BirthDate { get; set; }

}

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

[ServiceKnownType(typeof(Employee))]
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class GridWcfService
{
    [OperationContract]
    public ResultData GetDataAndCount(int startRowIndex, int maximumRows, string sortExpression, string filterExpression)
    {      
        if (filterExpression.Contains("BirthDate"))
        {
            var filterExpressionParts = filterExpression.Split('=');
            
            var filterDateValue = DateTime.Parse(filterExpressionParts[1].Replace("\\","").Replace("\"",""), new CultureInfo("da-DK").DateTimeFormat).ToShortDateString();
            var filterValue = String.Format("DateTime.Parse(\"{0}\")", filterDateValue);
            filterExpression = filterExpressionParts[0] + " =  " + filterValue;

            
        }

        GridBindingData data = RadGrid.GetBindingData("LinqToSql.NorthwindDataContext", "Employees", startRowIndex, maximumRows, sortExpression, filterExpression);
        ResultData result = new ResultData();
        result.Data = data.Data.OfType<LinqToSql.Employee>().Select(e => new Employee() {EmployeeID = e.EmployeeID, FirstName = e.FirstName, BirthDate = e.BirthDate}).ToList();
        result.Count = data.Count;
        return result;
    }
}    

