Mr. Trump complained about how Republicans fared late last year in Alabama, where Roy S. Moore, the party nominee he endorsed despite sexual abuse and child molestation accusations, lost his race to a Democrat, Doug Jones.

The president said he was relieved that the party might now avoid a similar fate in a West Virginia race that is among the most contested of the year. Days before a Republican primary there last week, Mr. Trump tweeted his opposition to Don Blankenship, a former coal mine operator who served a year in prison and campaigned on making racially insensitive comments about Mr. McConnell’s family. Mr. Blankenship lost the race.

“He was very optimistic and upbeat about it, and talked about the importance of us working hard and not screwing up,” Senator Johnny Isakson, Republican of Georgia, said of Mr. Trump’s assessment of the party’s chances in the midterms. “He’s getting better at this,” Mr. Isakson added.

Mike Braun, who won a Republican Senate primary in Indiana last week, was a guest at the lunch, where the president said he would keep returning to the state to campaign against “Sleepin’ Joe Donnelly,” his derisive name for the Democratic senator up for re-election there.

The comments as described by those who heard them appeared similar, in part, to the stump speech Mr. Trump delivered last week at a rally in Elkhart, Ind., where he told more than 7,000 supporters that Mr. Donnelly was a “swamp person,” and he boasted of his own achievements as president, including abandoning the Iran deal, making progress toward the talks with North Korea and saving money on the new embassy.

Mr. Trump has repeatedly claimed that he saved hundreds of millions of dollars on the new outpost in Israel, comparing its cost of $200,000 to $300,000 with the estimated $1 billion proposed for a new embassy. But the building dedicated Monday is a converted consular office that will serve as a temporary space until a new embassy, which will ultimately be more costly, can be constructed in Jerusalem.

Then, as an aside and in an apparent reference to the three college basketball players freed from China, Mr. Trump suggested during the lunch that no one in the room knew the harsh Chinese penalties for shoplifting better than Senator Richard M. Burr, Republican of North Carolina and the chairman of the Intelligence Committee.

It was not immediately clear what the president meant, but several senators laughed.