I'm learning C#, and I find LINQ absolutely interesting. However what is boggling me is, I can't think of a scenario whereby using LINQ would be an immense help, as its really not that hard replicating LINQ features in code.
Any personal experiences/suggestions you might wanna share?
Thanks!
I find that I'm using LINQ just about any time that I would have previously written a loop to fill a container. I use LINQ to SQL as my ORM and lots of LINQ everywhere else.
Here's a little snippet that I wrote for an Active Directory helper class that finds out if a particular user is an a particular group. Note the use of the Any() method to iterate over the user's authorization groups until it finds one with a matching SID. Much cleaner code than the alternative.
private bool IsInGroup( GroupPrincipal group, UserPrincipal user ) { if (group == null || group.Sid == null) { return false; } return user.GetAuthorizationGroups() .Any( g => g.Sid != null && g.Sid.CompareTo( group.Sid ) == 0 ); }
Alternative:
private bool IsInGroup( GroupPrincipal group, UserPrincipal user ) { if (group == null || group.Sid == null) { return false; } bool inGroup = false; foreach (var g in user.GetAuthorizationGroups()) { if ( g => g.Sid != null && g.Sid.CompareTo( group.Sid ) == 0 ) { inGroup = true; break; } } return inGroup; }
or
private bool IsInGroup( GroupPrincipal group, UserPrincipal user ) { if (group == null || group.Sid == null) { return false; } foreach (var g in user.GetAuthorizationGroups()) { if ( g => g.Sid != null && g.Sid.CompareTo( group.Sid ) == 0 ) { return true; } } return false; }
Here's a snippet that does a search against a repository, orders, and converts the first 10 matching business objects into a view-specific model (Distance
is the Levenshtein edit distance of the matching model's unique id from the uniqueID parameter).
model.Results = this.Repository.FindGuestByUniqueID( uniqueID, withExpired ) .OrderBy( g => g.Distance ) .Take( 10 ) .ToList() .Select( g => new GuestGridModel( g ) );
It depends on what kind of linq you mean.
Is it linq-to-sql? In that case, it's an orm with all the same benefits that come from using any other orm. I don't use it much and can't really say more.
Is it linq-to-objects? In that case, you're really talking about a collection of other things: extension methods, lazy iterators, and a query comprehension syntax. It's an introduction into the world of functional programming. I don't have much use for the query comprehension syntax, but for the rest, I can best demonstrate with an example.
Let's say you want to read a file in line by line. For each line, you want to check if it meets some criteria, convert a portion of those lines to an integer, and sum up the first 10 of those integers that are also within a certain range. Here's the old way you would do that:
int SumXValues(string filename) { string line; int sum = 0; int count = 0; using (var rdr = new StreamReader(filename)) { while ( (line = rdr.ReadLine()) != null && count < 10) { int val; if (int.TryParse(line.Substring(3,3)) { if (val > 4 && val < 25) { sum += val; count ++; } } } } return sum; }
Here's the new way:
IEnumerable<string> ReadLines(string filename) { string line; using (var rdr = new StreamReader(filename)) while ( (line = rdr.ReadLine()) != null) yield return line; } int SumXValues(string filename) { return ReadLines(filename) .Select(l => l.Substring(3,3)) .Where(l => int.TryParse(l)) .Select(i => int.Parse(i)) .Where(i => i > 4 && i < 16) .Take(10) .Sum(i => i); }
Notice the new code is actually shorter. But why is it also better? There are (at least) 4 reasons:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With