home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

C# + LINQ Help

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 2011
Discussions: 200,975, Posts: 2,379,804, Members: 246,339
Old June 29th, 2008, 12:48 AM   Digg it!   #1 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,398
C# + LINQ Help

ok i know I can do this with a List.Sort or with a crappy bubble sort loop.. but thats not nearly enough fun

So I'm trying to use LINQ which is supposed to be great for this stuff

So ultimate goal is to connect to a directory of a lot of files and get the oldest .log file w/o doing any loops.

So far I have

Code:
namespace PollExchangeProperties
{
    class Program
    {
        static void Main(string[] args)
        {

            DirectoryInfo di = new DirectoryInfo(@"C:\code\");
            foreach (DirectoryInfo d in di.GetDirectories())
            {
                Console.WriteLine("Directory: {0}", d.Name);
                Console.WriteLine("# Files: {0}", d.GetFiles("*").Length );
                foreach (FileInfo f in d.GetFiles())
                {
                    Console.WriteLine("FileName: {0}\\{1} = {2} kb", d.Name, f.Name, f.Length/1024);
                }
            }
       }
  }
}
This obviously uses a loop, but this is just for testing

So basically I want to do this

GetDirectory
Get all *.log files in directory and return date of oldest file in that directory

Yes I know it can be done with bat easily, but again thats not a challenge
This is a great excercise for me to learn C#
vass0922 is online now   Reply With Quote
Old June 29th, 2008, 01:37 AM     #2 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,398
if this were written in Ruby, this is exactly what I want it to do

Code:
def oldest_file(dir) 
  Dir.entries(dir).map { |e| 
    File.join(dir, e) 
  }.select { |f| 
    File.file? f 
  }.sort_by { |f| 
    File.mtime f 
  }.first 
end
vass0922 is online now   Reply With Quote
Old June 29th, 2008, 01:59 AM     #3 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,398
Found some code that works well

File System search via LINQ to Objects - Jaycent Drysdale
vass0922 is online now   Reply With Quote
Old June 29th, 2008, 09:19 AM     #4 (permalink)
Junior Member
 
Join Date: Jun 2008
Posts: 1
Post
How to Use LINQ to Filter a List of Files by Date

LINQ Exchange has a good article on sorting and filtering a list of files by date.
How to Use LINQ to Filter a List of Files by Date
merlin981 is offline   Reply With Quote
Old June 29th, 2008, 12:39 PM     #5 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,398
Excellent, thanks!

I'll make a note of the site as well.
vass0922 is online now   Reply With Quote
Old July 24th, 2008, 03:08 PM     #6 (permalink)
Junior Member
 
Join Date: Jul 2008
Posts: 2
Thumbs up

They have some other code over at linqhelp.com too!
tremendo is offline   Reply With Quote
Old November 9th, 2009, 06:56 AM     #7 (permalink)
Junior Member
 
Join Date: Nov 2009
Posts: 1
How to combine group by and orderby for LINQ in C#

I know how to orderby as shown below:

var query = from c in table.AsEnumerable()
orderby c.Field<string>("Subject"),
c.Field<string>("OwnerName") ascending
select c;

DataView dv = query.AsDataView();


But How to use LINQ in C# to do group by first and then orderby ? Finally, view result....

Thanks!!!
conanlive is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Health Care Rationing (6)
Is It Just Me? (3062)
Charges against non-tippers dropped.. (8)
Delete an OS (16)
Nvidia GTX 260 problem (9)
Laptop with wireless problem. (12)
Wireless Televisions. (12)
CPU fan stops spinning randomly (11)
windows vista security holes (17)
Regular Build (11)
Point and Shoot Camera Suggestions. (7)
windows 7 problem (7)
Internet Lost (5)
Multiple Restarts Required at Boot (5)
Recent Discussions
[F@H SPAM 11/16/09] ! 1/2 months to r.. (38)
Nvidia GTX 260 problem (9)
Dynex DX E-402 (3)
EVGA 9800 gtx help with finding a goo.. (12)
Multiple Restarts Required at Boot (5)
Point and Shoot Camera Suggestions. (7)
Delete an OS (16)
cell phone won't work (0)
Is the PSU I received dead? (15)
Can't open Word (12)
Steam ID's, Gamertags etc... (4)
Games, Cables, PCI cards, and more fo.. (6)
Dept. of HS: NSA 'Helped' Develop Vis.. (17)
Linksys WMP54GS wireless card problem.. (5)
windows vista security holes (17)
Help getting around port 80 for camer.. (5)
Skillsoft Network+ Study Software Que.. (10)
Browsers wont load websites (3)
help me pls laptop just stopped worki.. (0)
Open With ..... Win7 (3)
Laptop with wireless problem. (12)
Internet Lost (5)
virus blocking exe. files (1)
CPU fan stops spinning randomly (11)
Modern Warfare 2: Who Bought It? (65)


All times are GMT -4. The time now is 12:07 AM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28