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