我的物件的結構
int capacityNeeded, string preferedNeigborHood, string[] resourcesNeeded (object property is of type IEnumerable <string)
6, "Centro", new[] { "wi-fi", "tv" }
我的辦公室串列的結構
string LocationName, string Name, int MaxCapacity, IEnumerable AvailableResources
我的位置串列的結構string LocationName, string Name, int MaxCapacity, IEumerable AvailableResources
我需要將該物件與包含多個辦公室的串列進行比較,并通過回傳最近的辦公室來回傳包含偏好的辦公室,我的串列不一定包含相同的資源,而且包含不同的能力,我需要回傳最近的辦公室,而且我的辦公室串列沒有鄰域,但它有位置的名稱,位置串列有鄰域,我明白要實作的邏輯。但我真正需要的是如何撰寫代碼,例如,我可以獲得包含必要容量的辦公室,以及包含首選鄰居的位置,但我不知道如何獲得包含資源的辦公室,并考慮到它可能有我需要的資源和其他資源,例如,我的偏好是{"Wi-Fi","TV"},我有一個辦公室有{"Wi-Fi","TV","咖啡"}。我應該回傳那個辦公室,因為它有我的偏好。
我補充說,偏好的NeigborHood和resourcesNeeded都可以是空的
我想說的是,在我的辦公室里,我應該回傳那個辦公室,因為它有我的偏好。
我寫的一點代碼 uj5u.com熱心網友回復: 做了這個測驗專案,看看鏈接 相關問題的鏈接public IEnumerable< IOffice> GetOfficeSuggestion(SuggestionRequest suggestionRequest)/span>。
{
var resources = suggestionRequest.ResourcesNeeded;
if (resources.Count() == 0)
|| string.IsNullOrEmpty(suggestRequest.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,
可用資源 = office.AvailableResources
};
_suggestionsOffice.Add(list);
}
return _suggestionsOffice;
}
else[/span
{
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,
可用資源 = 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(suggestRequest.PreferedNeigborHood)
{
var officeSuggestion = offices.Where(x => x.MaxCapacity >= suggestionRequest.CapacityNeeded).OrderBy(o => o.MaxCapacity)。
return officeSuggestion。
}
else[/span
{
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; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/325722.html
標籤:
上一篇:型別錯誤:范圍物件不可呼叫
