Skip to content

Instantly share code, notes, and snippets.

@HamidMosalla
Created August 17, 2017 12:01
Show Gist options
  • Save HamidMosalla/f3269b19362b5be26a398a562a118bcb to your computer and use it in GitHub Desktop.
Save HamidMosalla/f3269b19362b5be26a398a562a118bcb to your computer and use it in GitHub Desktop.
public class Quote
{
public string Word { get; set; }
}
[Fact]
void LinqSingleMethod_WhenLazilyEvaluated_ReturnsDifferentInstance()
{
var quoteWords = new[]
{
new {Word = "Never"},
new {Word = "Attribute"},
new {Word = "To"},
new {Word = "Malice"},
new {Word = "That"},
new {Word = "Which"},
new {Word = "Is"},
new {Word = "Adequately"},
new {Word = "Explained"},
new {Word = "By"},
new {Word = "Stupidity"}
};
var transformedWithSelect = quoteWords.Select(c => new Quote { Word = c.Word });
var firstSingle = transformedWithSelect.Single(c => c.Word == "Adequately");
var secondSingle = transformedWithSelect.Single(c => c.Word == "Adequately");
Assert.NotSame(firstSingle, secondSingle);
}
[Fact]
void LinqSingleMethod_WhenNotLazilyEvaluated_ReturnsSameInstance()
{
var quoteWords = new[]
{
new {Word = "Never"},
new {Word = "Attribute"},
new {Word = "To"},
new {Word = "Malice"},
new {Word = "That"},
new {Word = "Which"},
new {Word = "Is"},
new {Word = "Adequately"},
new {Word = "Explained"},
new {Word = "By"},
new {Word = "Stupidity"}
};
var transformedWithSelect = quoteWords.Select(c => new Quote { Word = c.Word }).ToList();
var firstSingle = transformedWithSelect.Single(c => c.Word == "Adequately");
var secondSingle = transformedWithSelect.Single(c => c.Word == "Adequately");
Assert.Same(firstSingle, secondSingle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment