Introduction:
In this article,we are going to discuss about how to apply linq in Object datasource.
Main:
What is LINQ?
LINQ has a great power of querying on any source of data, data source could be the collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable<T> interface
For ex,
using System; using System.Data; using System.Configuration; using System.Collections; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq; public partial class Linq_To_Object : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { int[] fiboNumbers = new int[] { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; var data0 = from n in fiboNumbers where n % 2 == 0 select n; Response.Write(data0.Count()); Response.Write("<hr/>"); var data1 = (from n in fiboNumbers where n % 2 == 0 && n <10 select n).Sum(); Response.Write(data1); } } |
Conclusion:
Hope this helps,
Happy Coding.