C# Linq 如何使用对象的数据过滤列表并将结果存储在另一个列表中?

C# Linq How do I filter a list with the data of an object and store the result in another list?

我对象的结构

int capacityNeeded, string preferedNeigborHood, string[] resourcesNeeded(对象 属性 的类型为 IEnumerable

6, "Centro", new[] { "wi-fi", "电视" }

我的办公室列表结构

string LocationName, 字符串名称, 国际最大容量, IEnumerable 可用资源

我的位置列表的结构 字符串名称, 字符串邻居

我需要将该对象与包含多个办公室的列表和 return 包含偏好的办公室进行比较 return 最近的一个,我的列表不一定包含相同的资源并且包含不同的功能,我需要 return 最近的一个,我的办公室列表没有邻居,但它有位置名称,位置列表有邻居,我理解实现的逻辑,但是我真正需要的是如何编写代码,例如我可以获得包含必要容量的办公室,以及包含首选社区的位置,但我不知道如何获得包含资源的办公室,并考虑到它可能拥有我需要的资源和其他资源,例如我的偏好是 {"wi-fi", "tv"} 并且我的办公室有 {"wi-fi", "tv", " coffe"} 我应该 return 那个,因为它符合我的喜好。

我补充说 preferedNeigborHood 和 resourcesNeeded 都可以为 null

我写的一点代码

public IEnumerable<IOffice> GetOfficeSuggestion(SuggestionRequest suggestionRequest)
{
    var resources = suggestionRequest.ResourcesNeeded;

    if (resources.Count() == 0 
        || string.IsNullOrEmpty(suggestionRequest.PreferedNeigborHood))
    {
        var officeSuggestion = offices
                        .Where(x => x.MaxCapacity >= suggestionRequest.CapacityNeeded)
                        .OrderBy(o => o.MaxCapacity);

        foreach (var office in officeSuggestion)
        {
            var list = new OfficeDto
            {
                Name = office.Name,
                LocationName = office.LocationName,
                MaxCapacity = office.MaxCapacity,
                AvailableResources = office.AvailableResources
            };

            _suggestionsOffice.Add(list);
        }

        return _suggestionsOffice;
    }
    else
    {
        var officeSuggestion = offices
                        .Where(x => x.MaxCapacity >= suggestionRequest.CapacityNeeded)
                        .OrderBy(o => o.MaxCapacity);

        foreach (var office in officeSuggestion)
        {
            var list = new OfficeDto
            {
                Name = office.Name,
                LocationName = office.LocationName,
                MaxCapacity = office.MaxCapacity,
                AvailableResources = office.AvailableResources
            };

            _suggestionsOffice.Add(list);
        }

        return _suggestionsOffice;
    }
}

检查了这个测试项目link

    public class Program
{
    public static List<Office> offices = new List<Office>(){
    new Office{
    LocationName = "test",
    MaxCapacity = 6,
    AvailableResources = new[]{"wi-fi", "tv", "FHD", "Smth"}
    },
        new Office{
    LocationName = "test",
    MaxCapacity = 8,
    AvailableResources = new[]{"wi-fi", "tv", "FHD", "Smth"}
    }
    ,
    new Office{
    LocationName = "test",
    MaxCapacity = 3,
    AvailableResources = new[]{"wi-fi", "tv", "FHD", "Smth"}
    },
    new Office{
    LocationName = "test",
    MaxCapacity = 2,
    AvailableResources = new[]{"wi-fi", "Smth"}
    },
    new Office{
    LocationName = "test",
    MaxCapacity = 9,
    AvailableResources = new[]{"test","tv", "Smth"}
    },
    new Office{
    LocationName = "test",
    MaxCapacity = 7,
    AvailableResources = new[]{"wi-fi", "tv","Smth"}
    },
    new Office{
    LocationName = "test",
    MaxCapacity = 8,
    AvailableResources = new List<string>()
    }

    };
    
    public static void Main()
    {
        var officeSuggestion = GetOfficeSuggestion(new Request{
             CapacityNeeded= 6, PreferedNeigborHood="not used", ResourcesNeeded= new[] { "wi-fi", "tv" } });
        
        Console.WriteLine(officeSuggestion.ToList().Count.ToString());
        Console.ReadLine();
    }
    
     public static IEnumerable<Office> GetOfficeSuggestion(Request suggestionRequest)
    {
        var resources = suggestionRequest.ResourcesNeeded;
        var enumerable = resources.ToList();
        //Console.WriteLine(enumerable.ToList().Count.ToString());
        if (!enumerable.Any() || string.IsNullOrEmpty(suggestionRequest.PreferedNeigborHood))
        {
            var officeSuggestion = offices.Where(x => x.MaxCapacity >= suggestionRequest.CapacityNeeded).OrderBy(o => o.MaxCapacity);

            

            return officeSuggestion;
        }
        else
        {
            var officeSuggestion = offices.Where(x => x.MaxCapacity >= suggestionRequest.CapacityNeeded  &&  !enumerable.Except(x.AvailableResources).Any())
.OrderBy(o =>o.AvailableResources.Except(enumerable).Count()); 
            return officeSuggestion;
        }
    }
    
}

public class Office {

    public string LocationName{set; get;}
    public string Name{set; get;}
    public int MaxCapacity{set; get;}
    public IEnumerable<string> AvailableResources{set; get;}
}
public class Request {

    public string PreferedNeigborHood{set; get;}
    public int CapacityNeeded{set; get;}
    public IEnumerable<string> ResourcesNeeded{set; get;}
}

相关问题的链接

check whether an array is a subset of another

determine if a sequence contains all elements of another sequence using linq