Introduction
The LINQ is a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.
The Bellow class Use the Advantage og the LINQ througth the from,Select and where keys.IEnumerable data type is mainly used to handle the collections of items with different types.
// This class is used to Get the Picture Files from given Path and
// set the list of files as Data Table Using LINQ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
namespace WindowsFormsApplication1
{
class Helper
{
public String DirPath { get; set; }
public String FileType { get; set; }
public Helper(String sPath, String sFileType)
{
DirPath = sPath;
FileType = sFileType;
}
// To Get the IEnumerable type of Files List
public IEnumerableGetFiles()
{
try
{
// Simple LINQ in Files Systems
IEnumerableObjFilesList = from String sFile in Directory.GetFiles(DirPath, FileType, SearchOption.AllDirectories)
select sFile;
return ObjFilesList;
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
return null;
}
}
//To Get the Data Table
public DataTable GetDataTable()
{
IEnumerableObjlstFiles = null;
DataTable Objdt = new DataTable();
try
{
ObjlstFiles = GetFiles(); //"D:\\Siva\\Icons", "*.ico");
Objdt.Columns.Add("ICON File Names");
foreach (String sFileName in ObjlstFiles)
{
DataRow Objdr = Objdt.NewRow();
Objdr["ICON File Names"] = sFileName;//sFileName.Replace(DirPath, String.Empty);
Objdt.Rows.Add(Objdr);
}
return Objdt;
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
return null;
}
}
}
}
// Now we can get the DataTable and display in the DataGrid.
ObjHelper = new Helper(@"D:\Siva\Images\", "*.jpg");
Objdt = ObjHelper.GetDataTable();
if (Objdt.Rows.Count > 0)
{
grdvFiles.DataSource = Objdt.DefaultView;
grdvFiles.AutoResizeColumns();
grdvFiles.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}
else
{
lblMsg.Text = "Sorry!, No Picture File Found";
lblMsg.ForeColor = Color.Red;
}
Download the sample Application for Image Viewer Click Here



0 comments:
Post a Comment