Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

How return max value in LINQ, please?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ronald S. Cook - 04 Mar 2008 16:20 GMT
I have the following values in my AnimalTag table under the column
AnimalTagNumber.

123456
234567
345678
D00001
D00002
D00003

In LINQ, How can I select the Max (although it is not numeric) where the
first character is "D"?

I.e. for the above I'm wanting to return D00003.

Thanks for any assistance,
Ron
Jon Skeet [C# MVP] - 04 Mar 2008 16:40 GMT
> I have the following values in my AnimalTag table under the column
> AnimalTagNumber.
[quoted text clipped - 10 lines]
>
> I.e. for the above I'm wanting to return D00003.

You'll have to define exactly what you mean by "max" - lexicographic?

If you're happy with the behaviour of string in terms of
IComparable<string>, you can just call Max:

using System;
using System.Linq;

class Program
{
   static void Main(string[] args)
   {
       var words = new[]
       {            
           "123456",
           "234567",
           "345678",
           "D00001",
           "D00002",
           "D00003"
       };
       
       var max = words.Where(w => w[0]=='D')
                      .Max();
       
       Console.WriteLine(max);
   }
}

I'm somewhat surprised that you can't specify an IComparer<string> to
be honest - although it would be easy to write that as an extra
extension method.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.