The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  75 Posts :: 0 Stories :: 49 Comments :: 2 Trackbacks

Ads

Archives

Post Categories

Open Source Projects

Other Blogs

Of all the new features in VS2007, I would have to rate the LINQ stuff tops! I've put together a small example of using linq on a custom iterator. You can put the following code into a seperate class (I've called it LinqToDisk) 

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace LinqStuff
{
    class LinqToDisk : IEnumerable<FileInfo>
    {
        public string Folder
        {
            get; set;
        }
 
        public LinqToDisk() : this(@"C:\") { }
 
        public LinqToDisk(string folder)
        {
            this.Folder = folder;
        }
 
        #region IEnumerable<FileInfo> Members
        public IEnumerator<FileInfo> GetEnumerator()
        {
            foreach (FileInfo fileinfo in (new DirectoryInfo(Folder)).GetFiles())
            {
                yield return (fileinfo);
            }
        }
        #endregion
 
        #region IEnumerable Members
        System.Collections.IEnumerator 
		System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        #endregion
    }
}

This custom enumerator can then be used as follows:

        var resultSet = from fileInfo in new LinqToDisk() where 
		fileInfo.FullName.IndexOf(".bat") > 0 select fileInfo.FullName;
        foreach (var fileName in resultSet)
        {
            Console.WriteLine(fileName);
        }

As you can see from the query, I've select the FullName property of the FileInfo objects which are returned by the custom enumerator. I'll work on some more practical examples to post shortly.

posted on Thursday, June 21, 2007 6:07 PM

Feedback

# re: Linq to Disk Sample 7/4/2007 10:46 AM Salo
Schaweet! You may be interested to know the ADO.NET Entity Framework June 2007 CTP was released today :

http://www.microsoft.com/downloads/details.aspx?FamilyId=5C12FE07-E646-49C2-887F-8CC070B37247&displaylang=en

Comments have been closed on this topic.