Wednesday, July 30, 2008

LINQ - Select using Class

Beth Massi shows a way to project select data in class instances
Here the c# equivalent


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileSystem
{
public partial class Form1 : Form
{
class MyFile
{
private System.DateTime _creationTime;
private string _name;

public System.DateTime CreationTime
{
get {return _creationTime;}
set { _creationTime = value; }
}

public string Name
{
get { return _name; }
set { _name = value; }
}
}

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo root = new DirectoryInfo(".");

var myFiles = from FileInfo f in root.GetFiles()
select new MyFile { Name = f.Name,
CreationTime = f.CreationTime
};

dataGridView1.DataSource = myFiles.ToList();

}
}
}

No comments:

Post a Comment