Below is a simple demo of LINQ which prints all the items in the string array.
using System;
using System.Linq;
public partial class Linq_HelloLinq : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] greetings = { "Hello World", "Hello LINQ", "Hello" };
var greetingsList = from l in greetings select l;
foreach (var greeting in greetingsList)
{
Response.Write(greeting + "<br>");
}
}
}
using System.Linq;
public partial class Linq_HelloLinq : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] greetings = { "Hello World", "Hello LINQ", "Hello" };
var greetingsList = from l in greetings select l;
foreach (var greeting in greetingsList)
{
Response.Write(greeting + "<br>");
}
}
}
Important things to note are:
1. using System.Linq;
2. var greetingsList = from l in greetings select l;
Rest all is pretty straight forward.
After you are done with doing the above, try adding a where condition to it as below.
using System;
using System.Linq;
public partial class Linq_HelloLinq : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] greetings = { "Hello World", "Hello LINQ", "Hello" };
var greetingsList = from l in greetings where l.EndsWith("World") select l;
foreach (var greeting in greetingsList)
{
Response.Write(greeting + "<br>");
}
}
}
using System.Linq;
public partial class Linq_HelloLinq : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] greetings = { "Hello World", "Hello LINQ", "Hello" };
var greetingsList = from l in greetings where l.EndsWith("World") select l;
foreach (var greeting in greetingsList)
{
Response.Write(greeting + "<br>");
}
}
}
Thats it here ends our simple linq demo.