Skip to content

Instantly share code, notes, and snippets.

@OmerMor
Created October 24, 2011 19:41
Show Gist options
  • Save OmerMor/1309938 to your computer and use it in GitHub Desktop.
Save OmerMor/1309938 to your computer and use it in GitHub Desktop.
Some Random LINQ Snippet I needed
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