How to use Linq on a Dataset

Here’s an example of using Linq on a dataset table.

ICollection<MyDataSet. MyTableRow > rowsToDoSomethingWith = new List< MyDataSet.MyTableRow>(
from x in myDataSet.MyTable.AsEnumerable()
where x.Field<String>("Column1") == "Y"
select x);
Advertisements

List.ForEach Method

I think I’ve posted something related to this before, but it’s worth another look.  Use List().ForEach() to easily execute code inline.

        static void Main(string[] args)
        {
            List<int> myList = new List<int>() { 1, 2, 3, 4 };

            foreach (var i in myList)
                Console.WriteLine(i.ToString());

            Console.ReadLine();
        }

Becomes:

        static void Main(string[] args)
        {
            new List<int>() { 1, 2, 3, 4 }.ForEach(x => Console.WriteLine(x));
            Console.ReadLine();
        }

Functional Magic

Found an interesting article this morning about an interesting way to separate the implementation of a function from it’s declaration.

http://kohari.org/2007/08/23/functional-magic/