Re: Globarlly use var (from linq) or anything equivalent ?



Rich P wrote:
"files" here is a List<string> object. I retrieve a list of filenames
and then group them alphabetically -- like AA1, AA2, AA3, the AA group
has 3 files, then BB1, BB2, BB3, BB4 contains 4 files in the BB group,
.. using the linq below.

files = new List<string>(GetMyFiles(strPath));
char[] rgchDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};

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

foreach (var group in grouped)
Console.WriteLine(group.Key + " " + group.Count().ToString();

<<

Instead of using "var grouped = ... " locally inside a procedure, I
would like to populate a similar object that I can use globally. Here
is what I am trying to do: I retrieve a list of fileNames into files.
I then loop through files and display each fileName contained in files
on a label on a winform. The "var grouped = ... " list contains
information as follows:

AA 3
BB 4
CC 2
DD 7

The fileNames in files are groups of fileNames that contain the same
characters onto which I append a number. AA1, AA2, ... DD1, DD2,
..DD127, ...

As I loop through files -- I display the fileNames in the AA group and I
want to display the group count of files that contain AA alongside.
Then the BB's, ...

Theorectically I would have a table like this

Name Group Count
AA1 AA 3
AA2 AA 3
AA3 AA 3
BB1 BB 4
BB2 BB 4
BB3 BB 4
BB4 BB 4
CC1 CC 15
.. CC 15 CC15 CC 15
..

I want to display the individual fileNames and the group count from a
loop. One thought would be to populate an ADO.Net table with the
contents of "files" and "grouped" and loop through the table. But that
seems a little redundant. One challenge I am also seeing is to
correctly line up "files" with "grouped" so that the AAs counts don't
get mixed with HH counts for example.

On a real redundant note -- I could write the contents of "files" to a
sql server #temp table, parse out the characters of the filenames (to
the "group" column), then do a group by query and read all that back to
my ADO.net table. The downside of this idea is that I have thousands of
filenames to process (could take a while) and this adds a dependency on
a sql server.

Would a Dictionary fit into this scenario? How to implement?

Any suggestions would be appreciated how I could perform this operation
without introducy unnecessary redundancies and external dependencies.

Thanks,

Rich

You can easily put the group names and counts in a dictionary:

Dictionary<string, int> grouped = (
from file in files
let name = Path.GetFileName(file)
group file by name.Substring(0, name.IndexOfAny(rgchDigits))
).ToDictionary(g => g.Key, g => g.Count);

As you can find out the group from the file name, you can get the group information for each file:

foreach (string file in files) {
string name = Path.GetFileName(file);
string group = name.Substring(0, name.IndexOfAny(rgchDigits));
int count = grouped[group];
Console.WriteLine("{0}, {1}, {2}", name, group, count);
}

--
Göran Andersson
_____
http://www.guffa.com
.



Relevant Pages

  • Re: One more try to get help on 8.3 filenames in XP
    ... Access does display the filenme in long form. ... The app plays the file by opening Digital Orchestrator or setting focus if ... win 98 the file plays in XP I get a error as the file list ... website with short filenames. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Filenames and details blank in Windows Explorer until selected
    ... guess was right - the font colour was set to white since when the ... I wasn't interested in any of the display settings). ... The folder list in the left hand pane displays correctly, ... then filenames and details are displayed as expected. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: random number generator
    ... Jerry Coffin wrote: ... I plan on storing the filenames within an array ... Problem with shuffling is that the filenames have pairs so shuffling would ... I'm needing the display random, ...
    (alt.comp.lang.learn.c-cpp)
  • Displaying precise error using struts
    ... I have a webpage with a list of filenames. ... unique random number during its display on the UI ...
    (comp.lang.java.programmer)
  • WMP11 - Cannot get the most simple display : list of filenames in the file list
    ... I am sorting out music files, and I need a player where i can have a very simple display: the list of filenames which i have enqueued in the file list, and perhaps the classic player buttons at the top or at the bottom. ...
    (microsoft.public.multimedia.windows.mediaplayer)