Re: Globarlly use var (from linq) or anything equivalent ?
- From: Göran Andersson <guffa@xxxxxxxxx>
- Date: Wed, 17 Mar 2010 10:28:35 +0100
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
.
- References:
- Prev by Date: Re: timer
- Next by Date: Re: char ----> int
- Previous by thread: Re: Globarlly use var (from linq) or anything equivalent ?
- Next by thread: IIS Application Pools
- Index(es):
Relevant Pages
|