I have the below code that takes the files listed in a directory and the time the files were created. This is put in to a list via a struct I setup. I am having an issue trying to sort the list by the date it was created. I am new to development and need
some help with this.
public struct namesAndCreationDate
{
DateTime dt;
string fileName;
public namesAndCreationDate(DateTime dt1, string fn)
{
dt = dt1;
fileName = fn;
}
}
public static List<namesAndCreationDate> GetFileNames(string fileLocation)
{
List<namesAndCreationDate> inputDocNames = new List<namesAndCreationDate>();
var Num = 1;
var docs = Directory.GetFiles(fileLocation);
foreach (string item in docs)
{
if (Num <= docs.Length)
{
var dt = Directory.GetCreationTime(item);
namesAndCreationDate ncdate = new namesAndCreationDate(dt,item);
inputDocNames.Add(ncdate);
Num++;
}
else
{
Num -= 1;
}
}
///// Insert Linq here to sort List<>
return inputDocNames;
}
ByrdT
0 Points
10 Posts
Help Linq Query a List<struct>
Apr 30, 2012 04:28 PM|LINK
I have the below code that takes the files listed in a directory and the time the files were created. This is put in to a list via a struct I setup. I am having an issue trying to sort the list by the date it was created. I am new to development and need some help with this.
public struct namesAndCreationDate { DateTime dt; string fileName; public namesAndCreationDate(DateTime dt1, string fn) { dt = dt1; fileName = fn; } } public static List<namesAndCreationDate> GetFileNames(string fileLocation) { List<namesAndCreationDate> inputDocNames = new List<namesAndCreationDate>(); var Num = 1; var docs = Directory.GetFiles(fileLocation); foreach (string item in docs) { if (Num <= docs.Length) { var dt = Directory.GetCreationTime(item); namesAndCreationDate ncdate = new namesAndCreationDate(dt,item); inputDocNames.Add(ncdate); Num++; } else { Num -= 1; } } ///// Insert Linq here to sort List<> return inputDocNames; }pierrefrc
Participant
947 Points
201 Posts
Re: Help Linq Query a List<struct>
Apr 30, 2012 04:58 PM|LINK
Hi
You can use:
public struct namesAndCreationDate {ByrdT
0 Points
10 Posts
Re: Help Linq Query a List<struct>
Apr 30, 2012 07:03 PM|LINK
Thanks I forgot about the struct were private by default.
pierrefrc
Participant
947 Points
201 Posts
Re: Help Linq Query a List<struct>
Apr 30, 2012 07:42 PM|LINK