The train-to-yard assignment problem (TYAP) pertains to freight consolidation in a large rail transshipment yard—also called a multiple yard—that consists of two sub-yards. Inbound and outbound trains need to be assigned to one or the other sub-yard in a way that minimizes the total railcar switching costs. Each inbound and outbound train is processed in one of the two sub-yards, and time-consuming maneuvers may be necessary for railcars that are supposed to be part of an outbound train leaving from the other sub-yard. A lower number of railcar reassignments between the sub-yards reduce train dwell times and avoid train delays that affect the whole rail network. We develop a matheuristic algorithm with a learning mechanism, which we call MuSt, as well as a branch-and-bound procedure that incorporates elements of constraint propagation. We examine the performance of the developed algorithms through extensive computational experiments. Effective optimization approaches for the TYAP have high practical significance since they may reduce the number of avoidable railcar reassignments, which are resource-blocking, traffic-generating, and expensive, by about 20% compared to current practice, as we illustrate in our computational experiments. Our branch-and-bound algorithm solves problem instances for small or medium railyards in less than a minute or within several hours run time, respectively. The heuristic procedure MuSt finds optimal or nearly optimal solutions within just a couple of minutes, even for large railyards.