Sequence contains no elements LINQ error

I had a problem today that reminded me to expect the error you’re asking for (even if you don’t expect the error).

When using LINQ, .Single() and .SingleOrDefault() have different behaviors. Specifically if the result set is empty, .Single() will throw the error “Sequence contains no elements” but .SingleOrDefault() will return a null value. Expecting and handling one behavior but actually asking for the other means things are going to get messy.

Even though this particular piece of code is never supposed to come back empty, it inevitably did and resulted in the thrown error.

C#

// Throws poorly handled exception
var entity = dataConnection.Entity.Single(e =>
                             e.ID == Guid.Parse(entityID));

if (entity != null)
{ ... }

// Keeps going
var entity = dataConnection.Entity.SingleOrDefault(e =>
                             e.ID == Guid.Parse(entityID));

if (entity != null)
{ ... }
Share
facebooktwittergoogle_plusredditlinkedinfacebooktwittergoogle_plusredditlinkedin

Scroll a div until it reaches the top of the page

Problem
I have a div that always needs to be visible to the user. In this particular page design, having it simply be position: fixed; looks a little awkward as it leaves white-space above the control as the user scrolls. What I needed was something that lets the div scroll a little bit but stop once it hits the top of the page, and stick there.

Solution
By attaching a jquery function to the window’s scroll event I am able to update the div’s CSS based on the current location of the user’s scroll. This lets me set the pinned div’s position to fixed only once the div hits the top of the page. However, this has the effect of pulling that control div out of the document flow, so I simultaneously set the content div’s margin-left to the width of the control div (plus it’s normal margin) so it stays in place.

Demo on JSFiddle

JQuery

     $(function ($) {
        function pinDiv() {
            var $divToPin= $('.control_column');
            var $contentToAlign = $('.content_column');
            if ($(window).scrollTop() > 91) {
                $divToPin.css({
                    'position': 'fixed',
                    'top': '-1px'
                });
                $contentToAlign.css({
                    'margin': '0 0 0 182px'
                });
            } else {
                $divToPin.css({
                    'position': 'relative',
                    'top': 'auto'
                });
                $contentToAlign.css({
                    'margin': '0 0 0 10px'
                });
            }
        }
        $(window).scroll(pinDiv);
        pinDiv();
    });

HTML


<div class="header"><h1>Page Header</h1></div>
<div class="control_column">
    <button type="button">I'm a button!</button>
    <button type="button">Always show me!</button>
</div>
<div class="content_column">
 
     Scroll this happy talk away! 
<!-- Fill with long scrolling content -->

</div>

CSS

.content_column
{
     float: left;
     width: 400px;
     padding: 10px;
     background-color: #EAF0FF;
     border: 1px solid #98A3C5;
}
.control_column
{
     float: left;
     width: 150px;
     padding: 10px;
     background-color: #EAF0FF;
     border: 1px solid #98A3C5;
}
.header
{
     float: left;
     width: 100%;
     height: 90px;
}
button
{
    float: left;
    width: 100%;
}
Share
facebooktwittergoogle_plusredditlinkedinfacebooktwittergoogle_plusredditlinkedin

1k Legacy Tournament at Jupiter Games on 5/2/15

MANA DRAINOn May 2nd I split the Top 8 of a 35 man Legacy event at Jupiter Games.

I ran what is probably the most consistent UWr Miracles build, featuring a trim manabase and the full set of Ponders (see below).

Spent my winnings on an italian Mana Drain! Slotted it straight into my cube.

MATCH RESULTS

Opponent Win Loss Draw Match Result
Maverick 2 0 0 Win
Esper Thopters 2 0 0 Win
Miracles 0 2 0 Loss
Infect 2 1 0 Win
Elves 2 0 0 Win
ID (Miracles) 0 0 3 Draw

DECKLIST
UWr Miracles
2x Arid Mesa
4x Flooded Strand
4x Island
2x Plains
1x Polluted Delta
3x Scalding Tarn
3x Tundra
2x Volcanic Island

4x Brainstorm
2x Counterspell
1x Dig Through Time
4x Force of Will
4x Swords to Plowshares
1x Council’s Judgment
2x Entreat the Angels
4x Ponder
4x Terminus

4x Counterbalance
4x Sensei’s Divining Top

2x Snapcaster Mage
1x Vendilion Clique
2x Jace, the Mind Sculptor

SIDEBOARD
2x Containment Priest
1x Council’s Judgment
1x Counterspell
1x Elspeth, Knight-Errant
1x Engineered Explosives
1x Flusterstorm
2x Meddling Mage
2x Pyroblast
1x Red Elemental Blast
2x Vendilion Clique
1x Wear / Tear

Share
facebooktwittergoogle_plusredditlinkedinfacebooktwittergoogle_plusredditlinkedin