You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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:

Office Suggestion Matching: Full LINQ Implementation
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 use locations.Any(...) to link each office to its neighborhood via the LocationName property. This check only runs if PreferredNeighborhood is not null or empty—if the user didn't specify a preference, we skip this filter entirely.

  • Resource Full-Inclusion Matching:
    The All(...) method ensures every resource in ResourcesNeeded is present in the office's AvailableResources. If the user didn't request any resources (or the list is null), we bypass this check as required.

  • Cleanup & Refactoring:
    Removed redundant if/else blocks 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 locations collection is accessible in this method (class-level field, injected dependency, or method parameter).
  • Fixed the typo PreferedNeigborHood to match the request object's PreferredNeighborhood property—this would have caused silent failures otherwise!
  • If AvailableResources could be null, update the resource check to handle nulls safely: office.AvailableResources?.Contains(requestedResource) ?? false

内容的提问来源于stack exchange,提问作者DanielB.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:02:34