Posted by: Rhonda Tipton | November 29, 2007

LINQ Samples and Resources

LINQ stands for Language INtegrated Query. It is a native querying syntax that allows languages such as C# and VB.NET to filter and enumerate through collections. For example, arrays, data sets and relational databases.

Below are a couple of simple examples as well as some great resources.

Sample #1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] states = { "TX","LA","FL","AZ","TN","AK"};

            var stateList =
                from s in states
                select s;

            Console.WriteLine("State List:");
            foreach (var state in stateList)
            {
                Console.WriteLine(state);
            }
        }
    }
}

Result

Sample #2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] states = {"TX","LA","FL","AZ","TN","AK"};

            var stateList =
                from s in states
                where s.Substring(0,1) == "A"
                select s;

            Console.WriteLine("State List:");

            foreach (var state in stateList)
            {
                Console.WriteLine(state);
            }
        }
    }
}

Result

Great Resources for LINQ


Leave a response

You must be logged in to post a comment.

Categories