Follow by Email

Monday, September 30, 2013

using where condition like sql in .net | Linq query to fetch rows based on where condition

HI lets see how to fetch records in LINQ based on the where condition specified.


var query = from r in dtSampleDataTable.AsEnumerable()
                        where r.Field<string>("Salary") == "1000"
                        select r;

            if (query.Count() > 0)
                DataTable dt1 = query.CopyToDataTable(); //fetching filtered rows to a new datatable.
                object sumSalary = dt1.Compute("Sum(Salary)", "");

string s = sumSalary.ToString();