简体   繁体   English

C# Linq 如何使用 object 的数据筛选列表并将结果存储在另一个列表中?

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

Structure of my object我的object的结构

int capacityNeeded, string preferedNeigborHood, string[] resourcesNeeded (object property is of type IEnumerable <string) int capacityNeeded, string preferedNeigborHood, string[] resourcesNeeded(对象属性的类型为 IEnumerable <string)

6, "Centro", new[] { "wi-fi", "tv" } 6、“Centro”, new[] { “wi-fi”, “tv” }

Structure of my office list我的办公室列表的结构

string LocationName, string Name, int MaxCapacity, IEnumerable AvailableResources字符串 LocationName、字符串名称、int MaxCapacity、IEnumerable AvailableResources

Structure of my location list string Name, string Neighborhood我的位置列表结构string Name, string Neighborhood

I need to compare that object with a list containing multiple offices and return the office containing the preferences by returning the closest one, my list does not necessarily contain the same resources and contains different capabilities and I need to return the closest one, and my list of offices does not have the neighborhood, but it has the name of the location and the location list has the neighborhood, I understand the logic to implement, but what I really need is how to write the code, for example I can obtain the offices that contain the necessary capacity, as well as the location that contain the preferred neighborhood, but I don't know how to get the offices that contain the resources, and taking into account that it may have the resources I need and others, eg my preference is {"wi-fi", "tv"} and I have an office that has {"wi-fi", "tv", "coffe"} I should return that one since it has my preference.我需要将 object 与包含多个办公室的列表进行比较,并通过返回最近的办公室来返回包含首选项的办公室,我的列表不一定包含相同的资源并且包含不同的功能,我需要返回最近的办公室和我的列表of offices 没有 neighborhood,但是它有位置的名称,位置列表有 neighborhood,我理解实现的逻辑,但我真正需要的是如何编写代码,例如我可以获取 offices包含必要的容量,以及包含首选社区的位置,但我不知道如何获得包含资源的办公室,并考虑到它可能有我需要的资源和其他资源,例如我的偏好是 {"wi-fi", "tv"},我的办公室有 {"wi-fi", "tv", "coffe"} 我应该归还那个,因为它符合我的偏好。

I add that both the preferedNeigborHood and the resourcesNeeded can be null我补充说 preferedNeigborHood 和 resourcesNeeded 都可以是 null

A bit of the code i wrote我写的一些代码

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;
    }
}

made this test project check it out 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;}
}

links to related questions相关问题的链接

check whether an array is a subset of another 检查一个数组是否是另一个数组的子集

determine if a sequence contains all elements of another sequence using linq 使用 linq 确定一个序列是否包含另一个序列的所有元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM