Better way to write this code block? Suggestions please.

S

Sems

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);
}
}
}
 
J

Jason Keats

Sems said:
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/3319412/c-array-contains-partial
http://www.dotnettoad.com/index.php?/archives/10-Array.Contains.html
 
S

Sems

Sems said:
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.

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/archi.../index..php?/archives/10-Array.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);
}
}
 
U

Ulrik Magnusson

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;
 
S

Sems

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top