Menu ▼

Previous Next

LINQ to DataSet :

Input DataSet
dataset

LINQ Query to find Employee Ravi Expenses from Dataset :

var res = from exp in ds.Tables[0].AsEnumerable()

            where exp.Field<string>("EmployeeName") == "Ravi"

            select new

            {

                EmployeeName = exp.Field<string>("EmployeeName"),

                ExpenseAmount = exp.Field<decimal>("ExpenseAmount"),

                ExpenseDate = exp.Field<DateTime>("ExpenseDate").ToString("MM/dd/yyyy"),

                Expense = exp.Field<string>("Expense"),

                DepartmentName = exp.Field<string>("DepartmentName"),

            };

 

gvemployees.DataSource = res;

gvemployees.DataBind();


Output :
employee-ravi

LINQ Query to find Expenses from Dataset on 11/10/2013 :

var res = from exp in ds.Tables[0].AsEnumerable()

            where exp.Field<DateTime>("ExpenseDate") == new DateTime(2013, 11, 10)

            select new

            {

                EmployeeName = exp.Field<string>("EmployeeName"),

                ExpenseAmount = exp.Field<decimal>("ExpenseAmount"),

                ExpenseDate = exp.Field<DateTime>("ExpenseDate").ToString("MM/dd/yyyy"),

                Expense = exp.Field<string>("Expense"),

                DepartmentName = exp.Field<string>("DepartmentName"),

            };

 

gvemployees.DataSource = res;

gvemployees.DataBind();


Output :
expenses-on

Group By - LINQ Query to find expense amount from Dataset group by expense date :

var res = from exp in ds.Tables[0].AsEnumerable()

            group exp by exp.Field<DateTime>("ExpenseDate") into temp

            select new

            {

                Date = temp.Key.ToString("MM/dd/yyyy"),

                Amount = temp.Sum(s => s.Field<decimal>("ExpenseAmount"))

            };

 

gvemployees.DataSource = res;

gvemployees.DataBind();


Output :
group-by-date

Group By - LINQ Query to find total expense amount from Dataset on 11/10/2013 using group by :

var res = from exp in ds.Tables[0].AsEnumerable()

            where exp.Field<DateTime>("ExpenseDate") == new DateTime(2013, 11, 10)

            group exp by exp.Field<DateTime>("ExpenseDate") into temp

            select new

            {

                Date = temp.Key.ToString("MM/dd/yyyy"),

                Amount = temp.Sum(s => s.Field<decimal>("ExpenseAmount"))

            };

 

gvemployees.DataSource = res;

gvemployees.DataBind();


Output :
group-by-on-date
Previous Next




Search dotnetlearners.com