From: Peter Duniho on
Rich P wrote:
> [...]
> var grouped = from filename in myList
> group filename myPath
> .GetFilename(filename) <<<--- VS complains here
> .Substring(0, filename.IndexOfAny(_rgchDigits));
> }
>
> I appologize in advance for my ignorance on the subject of LinQ, but
> when I add your proposed code to the routine above - VS complains as
> noted.

As a general rule: do not write "VS complains". Provide a complete,
specific description of the exact error message.

That said, the problem in this particular case is obvious: you have
failed to copy the code I posted correctly. It's not "myPath". It's
"Path". As in, System.IO.Path (but seeing as how've used Directory and
other System.IO types without qualification, that means you have a
"using" directive, and "Path" by itself ought to be fine).

You've also left out the "by" keyword in the "group by" clause.

I do have a typo in my original post you'll need to fix though: the
method name is "GetFileName", and not "GetFilename".

> At this point in time I don't have enough experience/intuition
> to see what is missing or where to go next with the Linq part of the
> exercise.

You really should practice more dealing with compilation errors. The
errors usually are very specific, though in some cases overly
descriptive. When interpreted correctly, they practically always tell
you exactly what you need to know in order to fix the code.

Finally, I note you haven't changed your code over to use the List<T>
more efficiently:

� First, since you're creating a new one from scratch, you can simply
pass the "files" array to the constructor:

List<string> myList = new List<string>(files);

� Second, even if you didn't want to do that, you could use the
AddRange() method:

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

myList.AddRange(files);

� Third, there's nothing about your code that suggests you really
need to use a List<string> anyway, nor the "�Info" versions of the
System.IO classes. Why not write this:

private void GroupFiles()
{
string[] files = Directory.GetFiles(@"C:\1A\1AA\", "*.txt",
SearchOption.AllDirectories);

foreach (string str1 in files)
Console.WriteLine(str1);

char[] rgchDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9' };

var grouped = from filename in files
group filename by Path
.GetFileName(filename)
.Substring(0, filename.IndexOfAny(rgchDigits));
}

?

Pete
From: Rich P on
Wouldn't you know it? The day job had me doing something :) (Linq would
have helped out for this quicky proj - slapped together something -
which grouped data for another app).

Anyway -- Rock n Roll !

I am kinda starting to get the idea. Note: when I passed in the
string[] files array to var grouped = ... -- this included the path with
the filenames -- which the subdirectory names included digits in the
chars. So I didn't get the correct grouping I was looking for (only
printed: tes 7 one time in the console window).

So I went back to using FileInfo since that parses out the path part of
the filename and loaded up myList with the FileInfo filenames. Then I
passed myList to var grouped =....

Then I got the correct grouping --

test count = 4
testA count = 3

Yay!

I'm sure I could parse out the path from string[] files, but I am
thinking it is either some of one or some of the other --
files[i].Substring... or FileInfo something.

Many thanks for all the help and this great lesson. I am starting to
see some glimmer of light in my Linq pursuits :).


Rich

*** Sent via Developersdex http://www.developersdex.com ***
From: Peter Duniho on
Rich P wrote:
> Wouldn't you know it? The day job had me doing something :) (Linq would
> have helped out for this quicky proj - slapped together something -
> which grouped data for another app).
>
> Anyway -- Rock n Roll !
>
> I am kinda starting to get the idea. Note: when I passed in the
> string[] files array to var grouped = ... -- this included the path with
> the filenames -- which the subdirectory names included digits in the
> chars. So I didn't get the correct grouping I was looking for (only
> printed: tes 7 one time in the console window).

That doesn't make sense. The whole point of including the call to
Path.GetFileName() is to remove the path for the purpose of grouping.

> So I went back to using FileInfo since that parses out the path part of
> the filename and loaded up myList with the FileInfo filenames. Then I
> passed myList to var grouped =....
>
> Then I got the correct grouping --
>
> test count = 4
> testA count = 3

FileInfo doesn't do anything that you couldn't already do with
Path.GetFileName().

> Yay!
>
> I'm sure I could parse out the path from string[] files, but I am
> thinking it is either some of one or some of the other --
> files[i].Substring... or FileInfo something.

No. It's Path.GetFileName().

You can use FileInfo, but creating yet another object for a single
operation on the path when there's a static method to accomplish the
same thing is inefficient

Pete
From: Rich P on
>> I'm sure I could parse out the path from
>>string[] files, but I am thinking it is either some of
>>one or some of the other --
>>files[i].Substring... or FileInfo something.

>No. It's Path.GetFileName().

how would I implement/integrate

Path.GetFileName()

with

string[] files = Directory.GetFiles(@"C:\1A\1AA\", "*.txt",
SearchOption.AllDirectories);

char[] rgchDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};

var grouped = from filename in files
group filename by Path
.GetFileName(filename)
.Substring(0, filename.IndexOfAny(rgchDigits));




Rich

*** Sent via Developersdex http://www.developersdex.com ***
From: Peter Duniho on
Rich P wrote:
> how would I implement/integrate
>
> Path.GetFileName()
>
> with
>
> string[] files = Directory.GetFiles(@"C:\1A\1AA\", "*.txt",
> SearchOption.AllDirectories);
>
> char[] rgchDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
> };
>
> var grouped = from filename in files
> group filename by Path
> .GetFileName(filename)
> .Substring(0, filename.IndexOfAny(rgchDigits));

I don't understand the question. Path.GetFileName() is _already_
included in the code you posted. There's nothing more to do.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: WPF Dispatcher queues - race condition
Next: explicit cast