can Linq extract the largest numeric part from this string list?

Here is the list and the Linq I am using to generate it:

ADT.JPG
ADT1.JPG
ADT10.GIF
ADT2.JPG
ADT3.JPG
ADT4.JPG
ADT5.BMP
ADT6.JPG
ADT7.BMP
ADT8.JPG
ADT8A.JPG
ADT9.GIF

var names =
  from n in files
  where n.Contains("ADT")
  select Path.GetFileName(n);

foreach (string s in names)
Console.WriteLine(s);

the number I am looking for is "10" from

C:\1C\20CC\PICTURES\ADT10.GIF

I am looking for the largest numeric part of a filename.  My actual list
will also contain strings like this:

ADT83A.JPG

where I would want to extract 83 from ADT83A.JPG.  Can linq do this or
do I have to manually loop through this list?  

In pseudocode I am thinking something like this:

-----------------------------------------------
char[] rgchDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};
List<int> nums from n in names
            where n.Contains(numeric chars
                              from rgchDigits)
            select n;

var i =  select max(int) from nums;
-----------------------------------------------

can I do something like this with linq?  what would that look like?  

No need to consider strings like "ADT8XX3.BMP.  There is a convention
for this list where all numeric chars will be consecutive.

Thanks,

Rich

*** Sent via Developersdex http://www.developersdex.com ***
0
Rich
3/19/2010 3:40:26 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

2 Replies
697 Views

Similar Articles

[PageSpeed] 59

Rich P wrote:
> Here is the list and the Linq I am using to generate it:
> 
> ADT.JPG
> ADT1.JPG
> ADT10.GIF
> ADT2.JPG
> ADT3.JPG
> ADT4.JPG
> ADT5.BMP
> ADT6.JPG
> ADT7.BMP
> ADT8.JPG
> ADT8A.JPG
> ADT9.GIF
> 
> var names =
>   from n in files
>   where n.Contains("ADT")
>   select Path.GetFileName(n);
> 
> foreach (string s in names)
> Console.WriteLine(s);
> 
> the number I am looking for is "10" from
> 
> C:\1C\20CC\PICTURES\ADT10.GIF
> 
> I am looking for the largest numeric part of a filename.  

This should do:

             string[] names = {
  "ADT.JPG",
"ADT1.JPG",
"ADT10.GIF",
"ADT2.JPG",
"ADT3.JPG",
"ADT4.JPG",
"ADT5.BMP",
"ADT6.JPG",
"ADT7.BMP",
"ADT8.JPG",
"ADT8A.JPG",
"ADT9.GIF"
         };
             int max = (from name in names
                        let n = Regex.Replace(name, "[^0-9]+", "")
                        where n != ""
                        select int.Parse(n)).Max();


             Console.WriteLine(max);
-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/
0
Martin
3/19/2010 4:38:08 PM
Rich P wrote:

http://www.infoq.com/news/2007/09/LINQ-Aggregates

min, max, average -- etc, etc

I suggest you get a good book on Linq, like 'Linq in Action'.
0
Mr
3/19/2010 6:10:22 PM
Reply:

Similar Artilces:

can't send or receive emails
this just happened this week. I get this message....The connection to the server has failed. Subject 'help', Account: 'mail.comcast.net', Server: 'smtp.comcast.net', Protocol: SMTP, Port: 80, Secure(SSL): No, Socket Error: 10060, Error Number: 0x800CCC0E........your advice to to change smtp port to 25 or 587 or 80 does not work???? Port 80 is definitely wrong for that server; whose advice was that? Comcast wants you to use these email settings: http://www.comcast.com/Customers/FAQ/FaqDetails.ashx?Id=3D2288 Note that the outgoing server now requires port 5...

Access 97 can't resize database window
My database window with the listings of forms tables etc was adjusted to a smaller width, but resizing it is completely disabled and renders Access 2007 utterly useless for me. Is there anyway to 'reset' the window? ...

Can I copy radio buttons so that the second set is independent of.
I have a group of four radio buttons that all point to one cell. My goal is to create a second set that looks identical to the first but which points to a second cell. This is for a questionaire that will have many questions with the same four answer values. I want to be able to quickly create 100 button groups that will each update a separate cell for scoring of the questionaire. There are optionbuttons on the Control toolbox toolbar and there are optionbuttons on the Forms toolbar. Each has different behaviors. If I had to use lots, I'd use the Forms version. Here's a pos...

ignore list
I have importet some contact data into mscrm, When I want to add these contacts to a marketing list (add marketing list members / use advanced find/ add all selected members), the adding stops with an error. I have done a trace during the error (occurs everytime I want to add these contacts) which shows me the following error: [2009-08-24 11:15:36.778] Process:OUTLOOK |Thread:5884 |Category: Unmanaged.Platform |User: PlatformUser |Level: Error | Found crmId {319C876A-CC39-DC11-9F61-0030485C3892} in ignore list. Update notification will be ignored Function: CItemHelper<struct Outlook::_Co...

Drop-down list #5
I created a drop-down list in a separate sheet in my workbook. I named it Vehicles. Now I want to add to the list, but I can't figure out how to do it. I know ig must be so easy, but I'm stumped. Please help. If it's a one-time occurrence, you can press Ctrl+F3, click on the name, and extend the formula. Otherwise, I would make the range dynamic. You can learn how to do that here: http://www.contextures.com/xlNames01.html#Dynamic HTH Jason Atlanta, GA >-----Original Message----- >I created a drop-down list in a separate sheet in my workbook. I named it >Vehicles...

How can I get 32-bit Integers?
I am using Access MS Office 2007 with VBA 6.5, on Win XP. My Integer type is just 16 bits, i.e. its max value is 32,767. How can I configure it so as to have 32-bit Integers, along with 64-bit Longs? "Renny Bosch" <noname@nospam.com> wrote in message news:OqqNyqmuKHA.4492@TK2MSFTNGP05.phx.gbl... > I am using Access MS Office 2007 with VBA 6.5, on Win XP. My Integer type > is just 16 bits, i.e. its max value is 32,767. How can I configure it so > as to have 32-bit Integers, along with 64-bit Longs? An Access Integer is 16 bits. A Long Integer is...

Can SUMPRODUCT be used for entire column?
This formula results in a numeric result: =SUMPRODUCT((A1:A20)*(B1:B20="b")) but this formula results in a #NUM! result: =SUMPRODUCT((A:A)*(B:B="b")) Which means I need to specify the length of the columns, which may grow over time. Any way to do this for the entire column, without having to specify the length of the column? XL07 removed the limitation on array formulas (which SUMPRODUCT is, even though it doesn't require CTRL-SHIFT-ENTER) and entire columns. For pre-XL07, one can use =SUMPRODUCT(A1:A65535, --(B1:B65536="b")) to get all but on...

OE6 can't start due to message store prob
"Outlook Express could not be started. The application was unable to open the Outlook Express message store. Your computer may be out of memory or your disk is full (0x8007000E,5)" This happens after I try to copy in backed up .dbx files. ...

Can't create Organizational Forms Library in Exchange 2003 with SP
Hello, I cannot create an Organizational Form in EFORMS REGISTRY folder (from First Administrative Group->Folders->Public Folders->EFORMS REGISTRY in ESM). When I right-click the EFORMS REGISTRY folder and select New, there is no Organization Form. Instead, I only see Public Folder in the popup menu. Do you have any idea why Organizational Form menu does not show? My Exchange Server is Exchange 2003 with SP2. The login user is Administrator. Could you please help me? Thank you very much. Yang Is that account member of "Enterprise Admins" group? Yang Zhang wrote: &...

Can you delete Business Alerts?
I cannot see any way to delete Business Alerts, can someone tell me how? I am using GP 8.0 -- Sheri Salomone THANKS! Try going to Cards --> System --> Business Alerts. -- Charles Allen, MVP "Sheri Salomone" wrote: > I cannot see any way to delete Business Alerts, can someone tell me how? > I am using GP 8.0 > -- > Sheri Salomone > THANKS! woo hoo! Thank you! -- Sheri Salomone THANKS! "Charles Allen" wrote: > Try going to Cards --> System --> Business Alerts. > -- > Charles Allen, MVP > > > > "Sheri Salo...

Office 2007 forms
I am creating a form with office 2007, will those people who do not use office 2007 be able to fill in my form? should I save it in a particular format? thanks Provided you start from the normal template, don't use fonts that were introduced with Word 2007, and save the form in Word 97-2003 document format, anyone with Word 97 or later should be able to open it. Use only the legacy form fields, to which end http://gregmaxey.mvps.org/Classic%20Form%20Controls.htm will make things easier. -- <>>< ><<> ><<> <>>< ><<...

Can the data in a chart table be right justified?
Ecxel 2003 and previous versions of the product center the data in the data columns. Can the data in the columns of a chart table be right justified? In article <DABF738B-6C0D-458B-B082-FA9BD8F126A7@microsoft.com>, =?Utf- 8?B?c2FtIGVhZ2xl?= <sam eagle@discussions.microsoft.com> says... > Ecxel 2003 and previous versions of the product center the data in the data > columns. Can the data in the columns of a chart table be right justified? > Have you tried to format the table? If yes, and you haven't been successful it is probably because XL allows very limited cust...

can I snap wrap points to a text box
rather than having to add individual wrap points to the edge of a frame, which is never as accurate anyway, can they be set to 'snap' to a frame (eg the ellipse) so that they are perfectly inline, (and which would of course be a lot quicker)? Edit points will not snap. There are options for edit points, select a point, right click. If you hold down control, the cursor will turn into an x, you can delete a point with a click. Truly a good draw program would be preferable. -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com &q...

Global distribution List Not Appearing in Outlook Global Selections list
I have created a global list in AD in Server 2003 ( using Exchange 2003) and assigned my users. For some reason the list is not showing in the Outlook client global address list. I have compared this list to others i have created in the past and cannot spot any differences. What am I missing? 1. Have you got it hidden from the GAL? 2. Using cached Exchange mode? Therefore requiring a rebuild of the OAB to see it? Oliver On Fri, 16 Feb 2007 13:24:05 -0500, "rlm" <rmorton@execpc.com> wrote: >I have created a global list in AD in Server 2003 ( using Exchange 2003...

Why ClassWiard can't see my Class?
I created a class(Class-A), derived a class(Class-B) which derived from CWnd class. When I used mouse right-click on the new-created class, it shows a popup-command-menu which missing "add virtual function" and "add windows message handler". From ClassWizard I couldn't see the new-created class either. I tried to delete myprogram.clw & re-issue ClassWizard, but it doesn't help. Anyone can help ? Thanks, Eagle "EagleChen" <xx@xx> wrote in message news:uRGde941DHA.3496@TK2MSFTNGP11.phx.gbl... > I created a class(Class-A), derived a class(C...

VCR to DVD Part 2
Many thanks for previous answers. Yes, finalizing the DVD worked I found that DVD-R need finalizing but DVD+RW disks do not But why was I able to play the unfinalized DVD-R on one machine and not on two others. What was special about the first PC? -- www.stfx.ca/people/bliengme Bernard Liengme wrote: > Yes, finalizing the DVD worked > I found that DVD-R need finalizing but DVD+RW disks do not > But why was I able to play the unfinalized DVD-R on one machine and not on > two others. > What was special about the first PC? Almost anything will play a finalised D...

pulling certain characters from a string of text
I need to look up "certain critera" within a string of characters, then return that "certain criterea" to a new column. Some examples of a strings of characters may look like these: K5J091509001 Sample PO#S881009 K55sample PO CarrieRJR TJ5 My "Certain Critera" I have listed on another sheet, named "REP ID" K5J S88 K55 RJR TJ5 How do I pull out the 3 characters of "Certain Criterea" from the string of text and copy or enter it into a new column? Hi, =left(a1,3) "SaraMack" wrote: > I need to look up "c...

Publisher can not save file
I recently started having troubles with my Publisher 2003. Whenever I go to save my files now, using save as or just the save button, it gives me a dialog box that says "Can not save file." It does that twice, then it disappears. I also noticed that when it does this, it leaves the .tmp files in the directory where I tried to save. I can change the name of the files, and sometimes it will save it. Most of the time not though. I have NAV, and I noticed that it was said there was an issue with Publisher and NAV. Is this the same with the 2005 version, and is this anything anyone...

how can i edit the positioning of the balloon comment in a word fi
how can i edit the positioning of the balloon comment in a microsoft word file ? please reply on my email What you can do is adjust the space reserved for the balloons in the margin. In Word 2007, on the Review tab, click Track Changes, and then click Change Tracking Options. Change the "Preferred width" setting. -- Stefan Blom Microsoft Word MVP "melikelmalik" <melikelmalik@discussions.microsoft.com> wrote in message news:80E5F3D3-04A0-4E81-B154-FA8459B25F00@microsoft.com... > how can i edit the positioning of the balloon comment in a mi...

Report Can Grow not aligned horizontally
I have a report where I have 1 row of text, memo, number, and date fields. The memo field can contain up to 4-5 lines of wrapped text. I have set all teh rows to "can grow". However, on my report, the conditional formatting doesn't grow. For instance, the fill (which I have set to a gray color) in the memo field is about 3 lines deep when there is a lot of text in the memo field, but the text and number fields associated with that record are actually only 1 line. This essentially looks like a mess, small gray filled boxes for the text fields and a large gray ...

Customize columns in 'Marketing List Members'
I can't figure out where one can customize the columns used within the "marketing list" entitry when you click the 'marketing list members' on the left side to show the members. I want to add some columns, like Email. Screenshot: http://i355.photobucket.com/albums/r469/canadaka_bucket/marketing_list_members.jpg Just read the Posting on the Microsoft CRM Team Blog. <canadaka@gmail.com> schrieb im Newsbeitrag news:306584c6-2043-4962-b12a-d0b9287684bb@b31g2000prb.googlegroups.com... > I can't figure out where one can customize the columns used within the >...

How can I print on 8 1/2"x17" paper?
I have cut down an 11x17 paper to 8 1/2" x 17". I cannot figure out how to change the paper size to accept and print anything bigger than 14." any suggestions would be greatly appreciated First make the size settings in the Printer Setup. You may need to use the Custom selection. -- Don Vancouver, USA "mickilena" <mickilena@discussions.microsoft.com> wrote in message news:1AD99AFE-7FE7-4A07-9D20-1B1AFF9856BD@microsoft.com... >I have cut down an 11x17 paper to 8 1/2" x 17". I cannot figure out how to > change the paper size to accept a...

how can I do a lookup into another sheet?
how can I do a lookup into another sheet? thanks ------------------------------------------------ ~~ Message posted from http://www.ExcelTip.com/ ~~View and post usenet messages directly from http://www.ExcelForum.com/ Try something like =VLOOKUP(123,Sheet2!A1:B10,2,FALSE) -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com "melawaisi" <melawaisi.ws67m@excelforum-nospam.com> wrote in message news:melawaisi.ws67m@excelforum-nospam.com... > > how can I do a lookup into another sheet? > thanks > > > ------...

Can't re-enter a previously deleted User ID
We changed the spelling of a User ID (applewicks to appelwicks) and then deleted it (since he couldn't remember his password and the button for password was greyed out so we couldn't change it.) And now we can't re-enter the same user ID even though it doesn't appear in the window any longer. Here is the error we get: ODBC SQL server driver: The log in appelwicks already exists. Thanks! I believe you have to delete the old ID through Enterprise Manager as well. "cliffs" wrote: > We changed the spelling of a User ID (applewicks to appelwicks) and then ...

Refreshing list boxes
Hi, I have created a database so that mulpile users can add detailss to the table - tblDetails from the form frmDetails or amend details in form frmAmend. I have a list box on the main page frmMain which has a list box lstSearch which shows all the records in the database. When users add new record to the database other users cannot see the added records unless the move to another section in the datbase and the return to the main page thus refreshing the list box. Can anyone tell me if there is a way I can put a "Refresh" button on the main page that updated the list box if the use...