“Gershon’s chance to pull off the upset hinges upon the fact that the polls show there are choppy seas for both sides, but no partisan tide, whereas for two years Zeldin has acted like this district is a Trump island,” said Bruce Gyory, a Democratic political consultant who is not involved in the race.

“The irony,” he added, “is that this year, the voters seem to be in the same mood they were when they tossed Bishop out and elected Zeldin.”

While he jump-started his campaign with his own money (he put $1.3 million into the primary race), Mr. Gershon has since garnered widespread support among Democrats, aided by the groundswell of anti-Trump sentiment.

And he has sought to portray Mr. Zeldin’s sharp conservatism as out of touch with a district where, he said, many Republicans and Democrats consider themselves centrists. Mr. Zeldin’s voting record, in Albany as a state legislator, and in Congress, leaves no doubt as to his views.

He has voted against same-sex marriage, funding Planned Parenthood and the Dream Act, which would provide tuition assistance to undocumented immigrants. He voted to repeal and replace the Affordable Care Act, to expand the right to carry concealed firearms anywhere in the country, and to permit Americans to deny services to same-sex couples on religious grounds.

His scorecard includes a 9 percent from the League of Conservation Voters and an A from the National Rifle Association. Mr. Zeldin also opposed a bill that would help law enforcement prevent those on terror watch and no-fly lists from legally buying guns.