PC Review


Reply
Thread Tools Rate Thread

Better way to write this code block? Suggestions please.

 
 
Sems
Guest
Posts: n/a
 
      3rd May 2011
Hi all

I'm looking for some feedback / suggestions on how to write a piece of
code in a better way.
The below code works but I want to write it using query syntax to to
get rid of the nested loops. How would I do this?

The code builds up a list of urls that do not end in the extentions
listed in the nonIncludedExts strings.

I know this can be done in a cleaner way but am not sure of the best
approach.

Thanks



List<string> parsedData = new List<string>();

using (StreamReader readFile = new StreamReader(path))
{
string nonIncludedExts =
".js .gif .css .axd .png .txt .ico .flv .jpg .swf"
string[] splitExts = nonIncludedExts.Split(new
char[] { ' ' });
string line;

while ((line = readFile.ReadLine()) != null)
{
bool endsWithExcludedExt = false;

foreach (string ext in splitExts)
{
if (line.EndsWith(ext))
endsWithExcludedExt = true;
}

if (!endsWithExcludedExt)
{
parsedData.Add(line);
}
}
}
 
Reply With Quote
 
 
 
 
Jason Keats
Guest
Posts: n/a
 
      3rd May 2011
Sems wrote:
> Hi all
>
> I'm looking for some feedback / suggestions on how to write a piece of
> code in a better way.
> The below code works but I want to write it using query syntax to to
> get rid of the nested loops. How would I do this?
>
> The code builds up a list of urls that do not end in the extentions
> listed in the nonIncludedExts strings.
>
> I know this can be done in a cleaner way but am not sure of the best
> approach.
>
> Thanks
>
>
>
> List<string> parsedData = new List<string>();
>
> using (StreamReader readFile = new StreamReader(path))
> {
> string nonIncludedExts =
> ".js .gif .css .axd .png .txt .ico .flv .jpg .swf"
> string[] splitExts = nonIncludedExts.Split(new
> char[] { ' ' });
> string line;
>
> while ((line = readFile.ReadLine()) != null)
> {
> bool endsWithExcludedExt = false;
>
> foreach (string ext in splitExts)
> {
> if (line.EndsWith(ext))
> endsWithExcludedExt = true;
> }
>
> if (!endsWithExcludedExt)
> {
> parsedData.Add(line);
> }
> }
> }



http://www.tkachenko.com/blog/archives/000682.html
http://stackoverflow.com/questions/3...ntains-partial
http://www.dotnettoad.com/index.php?....Contains.html
 
Reply With Quote
 
Sems
Guest
Posts: n/a
 
      3rd May 2011
On May 3, 2:39*pm, Jason Keats <jke...@melbpcDeleteThis.org.au> wrote:
> Sems wrote:
> > Hi all

>
> > I'm looking for some feedback / suggestions on how to write a piece of
> > code in a better way.
> > The below code works but I want to write it using query syntax to to
> > get rid of the nested loops. How would I do this?

>
> > The code builds up a list of urls that do not end in the extentions
> > listed in the nonIncludedExts strings.

>
> > I know this can be done in a cleaner way but am not sure of the best
> > approach.

>
> > Thanks

>
> > List<string> *parsedData = new List<string>();

>
> > using (StreamReader readFile = new StreamReader(path))
> > * * * * * * * * *{
> > * * * * * * * * * * *string nonIncludedExts =
> > ".js .gif .css .axd .png .txt .ico .flv .jpg .swf"
> > * * * * * * * * * * *string[] splitExts = nonIncludedExts.Split(new
> > char[] { ' ' });
> > * * * * * * * * * * *string line;

>
> > * * * * * * * * * * *while ((line = readFile.ReadLine()) != null)
> > * * * * * * * * * * *{
> > * * * * * * * * * * * * *bool endsWithExcludedExt = false;

>
> > * * * * * * * * * * * * *foreach (string ext in splitExts)
> > * * * * * * * * * * * * *{
> > * * * * * * * * * * * * * * *if (line.EndsWith(ext))
> > * * * * * * * * * * * * * * * * *endsWithExcludedExt = true;
> > * * * * * * * * * * * * *}

>
> > * * * * * * * * * * * * *if (!endsWithExcludedExt)
> > * * * * * * * * * * * * *{
> > * * * * * * * * * * * * * * *parsedData.Add(line);
> > * * * * * * * * * * * * *}
> > * * * * * * * * * * *}
> > * * * * * * * * *}

>
> http://www.tkachenko.com/blog/archiv...Contains.html- Hide quoted text -
>
> - Show quoted text -


Great, thanks, I've got it down to this now...

List<string> parsedData = new List<string>();

using (StreamReader readFile = new StreamReader(path))
{
string nonIncludedExts =
ConfigurationManager.AppSettings["excludedExtentions"];
string[] splitExts = nonIncludedExts.Split(new
char[] { ' ' });
string line;

while ((line = readFile.ReadLine()) != null)
{
bool endsWithExcludedExt = splitExts.Any(x =>
line.EndsWith(x));

if (!endsWithExcludedExt)
parsedData.Add(line);
}
}

 
Reply With Quote
 
Ulrik Magnusson
Guest
Posts: n/a
 
      3rd May 2011
If the file is small, you could read it using ReadAllLines and do
this:

string nonIncludedExts =
ConfigurationManager.AppSettings["excludedExtentions"];
string[] splitExts = nonIncludedExts.Split(' ');

IEnumerable<string> res =
from line in File.ReadAllLines(path)
where !splitExts.Any(x => line.EndsWith(x))
select line;
 
Reply With Quote
 
Sems
Guest
Posts: n/a
 
      4th May 2011
On May 3, 10:54*pm, Ulrik Magnusson <ulrik...@gmail.com> wrote:
> If the file is small, you could read it using ReadAllLines and do
> this:
>
> string nonIncludedExts =
> ConfigurationManager.AppSettings["excludedExtentions"];
> string[] splitExts = nonIncludedExts.Split(' ');
>
> IEnumerable<string> res =
> * * * * * * * * *from line in File.ReadAllLines(path)
> * * * * * * * * *where !splitExts.Any(x => line.EndsWith(x))
> * * * * * * * * *select line;


Great stuff but the file has over 180k of lines in it.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
block, end, async read/write cause delay? Ryan Liu Microsoft C# .NET 1 20th May 2008 12:22 PM
Read/Write to storage class device using sector/block addresses Arnie Windows XP Embedded 0 21st Apr 2008 09:42 PM
Can NetworkStream.Write() block? Gaz Microsoft C# .NET 3 19th Jan 2008 08:15 PM
CD writer will not write, suggestions? =?Utf-8?B?Y29kZTkw?= Windows XP Help 8 27th Sep 2006 03:54 PM
Exception manegement application block can't write to Windows server 2003 Lucas Microsoft ASP .NET 7 6th Dec 2003 07:14 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:21 AM.