繁体   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?

我的object的结构

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

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

我的办公室列表的结构

字符串 LocationName、字符串名称、int MaxCapacity、IEnumerable AvailableResources

我的位置列表结构string Name, string Neighborhood

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

我补充说 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;
    }
}

让这个测试项目检查链接

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

相关问题的链接

检查一个数组是否是另一个数组的子集

使用 linq 确定一个序列是否包含另一个序列的所有元素

暂无
暂无

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

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