如何使用C# LINQ根据请求对象筛选办公室列表并返回最匹配结果
Here's a complete implementation that addresses all your requirements, including the missing resource inclusion check and neighborhood association filtering. I've also refactored redundant code to keep it clean:
public IEnumerable<OfficeDto> GetOfficeSuggestion(SuggestionRequest suggestionRequest) { _suggestionsOffice.Clear(); // Prevent accumulation of old results var filteredOffices = offices // 1. Basic capacity check: Office must meet or exceed needed capacity .Where(office => office.MaxCapacity >= suggestionRequest.CapacityNeeded) // 2. Neighborhood filter (only applied if preference is provided) .Where(office => string.IsNullOrEmpty(suggestionRequest.PreferredNeighborhood) || locations.Any(location => location.Name == office.LocationName && location.Neighborhood == suggestionRequest.PreferredNeighborhood)) // 3. Resource inclusion check (only applied if resources are requested) .Where(office => suggestionRequest.ResourcesNeeded == null || !suggestionRequest.ResourcesNeeded.Any() || suggestionRequest.ResourcesNeeded.All(requestedResource => office.AvailableResources.Contains(requestedResource))) // 4. Sort by closest matching capacity (smallest sufficient capacity first) .OrderBy(office => office.MaxCapacity - suggestionRequest.CapacityNeeded); // Map filtered offices to DTOs foreach (var office in filteredOffices) { _suggestionsOffice.Add(new OfficeDto { Name = office.Name, LocationName = office.LocationName, MaxCapacity = office.MaxCapacity, AvailableResources = office.AvailableResources }); } return _suggestionsOffice; }
Key Logic Explanations:
Neighborhood Association:
We uselocations.Any(...)to link each office to its neighborhood via theLocationNameproperty. This check only runs ifPreferredNeighborhoodis not null or empty—if the user didn't specify a preference, we skip this filter entirely.Resource Full-Inclusion Matching:
TheAll(...)method ensures every resource inResourcesNeededis present in the office'sAvailableResources. If the user didn't request any resources (or the list is null), we bypass this check as required.Cleanup & Refactoring:
Removed redundantif/elseblocks since all conditional filters can be combined directly in the LINQ query. Added_suggestionsOffice.Clear()to avoid leftover results from previous calls (adjust this if your list is scoped differently).Closest Capacity Sorting:
OrderBy(office => office.MaxCapacity - suggestionRequest.CapacityNeeded)sorts offices by the smallest gap between their maximum capacity and the user's need, giving you the most precise match first.
Quick Notes:
- Ensure the
locationscollection is accessible in this method (class-level field, injected dependency, or method parameter). - Fixed the typo
PreferedNeigborHoodto match the request object'sPreferredNeighborhoodproperty—this would have caused silent failures otherwise! - If
AvailableResourcescould be null, update the resource check to handle nulls safely:office.AvailableResources?.Contains(requestedResource) ?? false
内容的提问来源于stack exchange,提问作者DanielB.

