Created
October 24, 2011 19:41
-
-
Save OmerMor/1309938 to your computer and use it in GitHub Desktop.
Some Random LINQ Snippet I needed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
namespace LinqSnippet | |
{ | |
public class LinqSnippet | |
{ | |
public void Test() | |
{ | |
var oldlines = new[] {"xxx", "a b 1", "a c 2", "b a 3"}; | |
var newlines = new[] {"a b 2", "a c 2", "b b 1"}; | |
var results = | |
from oldline in oldlines.ToDict() | |
join newline in newlines.ToDict() | |
on oldline.Key equals newline.Key | |
where oldline.Value != newline.Value | |
select oldline.Key; | |
var x = results.ToArray(); | |
} | |
} | |
public static class EnumerableEx | |
{ | |
public static Dictionary<string, string> ToDict(this IEnumerable<string> oldlines) | |
{ | |
var regex = new Regex(@"^(\w+\s+\w+)\s+(\w+)$"); | |
var oldMatches = oldlines | |
.Select(line => regex.Match(line)) | |
.Where(match => match.Success) | |
.Select(match => match.Groups | |
.Cast<Group>() | |
.Select(group => group.Value) | |
.ToArray()) | |
.ToDictionary(groups => groups[1], groups => groups[2]); | |
return oldMatches; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment