Structure of my object
int capacityNeeded, string preferedNeigborHood, string[] resourcesNeeded (object property is of type IEnumerable <string)
6, "Centro", new[] { "wi-fi", "tv" }
Structure of my office list
string LocationName, string Name, int MaxCapacity, IEnumerable AvailableResources
Structure of my location list 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.
I add that both the preferedNeigborHood and the resourcesNeeded can be 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;
}
}
CodePudding user response:
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.MaxCapacity);
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