Skip to content

Instantly share code, notes, and snippets.

@ezesundayeze
Last active October 10, 2016 00:50
Show Gist options
  • Save ezesundayeze/355d30d6bd52460b8e762e9dff8a23ef to your computer and use it in GitHub Desktop.
Save ezesundayeze/355d30d6bd52460b8e762e9dff8a23ef to your computer and use it in GitHub Desktop.
public static void Main (string[] args)
{
// lets assume this is our database, here is a simple list of names
List<string> myList = new List<string> ();
myList.Add ("eze");
myList.Add ("joy");
myList.Add ("akwa");
myList.Add ("sunday");
// and here is our linq query
var query = from r in myList
where r.Length<=3
select r;
// and here is our main foreach loop. Using the using statement to //dispose our database resources
//And the while loop to loop through each and every item as specified //in our linq query.
using (var enumeratorr = query.GetEnumerator ()) {
while (enumeratorr.MoveNext ()) {
var element = enumeratorr.Current;
Console.WriteLine (element);
}
// here is the foreach version
foreach (var item in query) {
Console.WriteLine (item);
}
//You can see that with this, you write short,readable and scalable code, you are rest assured your code will work without flaws and will use and dispose resource as required.
}
Console.WriteLine ("Hit Any Key To Continue...");
Console.ReadKey ();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment