我在一个页面上工作,用户可以在其中过滤展示事件列表。为此,他们可以从三个标准中进行选择:月份,位置,事件名称。
对于每个条件,通过jquery ajax创建并发布字符串数组。降低数组,然后筛选show事件列表:
if (loweredAndTrimmedDates != null)
{
listFilteredByDates = new List<Performance>();
listFilteredByDates = performanceList.Where
(
performance =>
loweredAndTrimmedDates.Contains(performance.Datum.Value.ToString("MMMM", CultureInfo.DefaultThreadCurrentCulture).ToLower().Trim())
).ToList();
if (loweredAndTrimmedLocations != null)
{
listFilteredByLocations = new List<Performance>();
listFilteredByLocations = listFilteredByDates.Where
(
performance =>
loweredAndTrimmedLocations.Any(location => location.Equals(performance.Location.Name.ToLower().Trim() + ", " + performance.Location.Stadt.ToLower().Trim()))
).ToList();
if (loweredAndTrimmedEvents != null)
{
listFilteredByEvents = new List<Performance>();
listFilteredByEvents = listFilteredByLocations.Where
(
performance =>
loweredAndTrimmedEvents.Any(evnt => evnt.Contains(performance.Event.Name.ToLower().Trim()))
).ToList();
return listFilteredByEvents.OrderBy(p => p.Datum);
}
else
{
return listFilteredByLocations.OrderBy(p => p.Datum);
}
}
else
{
if (loweredAndTrimmedEvents != null)
{
listFilteredByEvents = new List<Performance>();
listFilteredByEvents = listFilteredByDates.Where
(
performance =>
loweredAndTrimmedEvents.Any(evnt => evnt.Contains(performance.Event.Name.ToLower().Trim()))
).ToList();
return listFilteredByEvents.OrderBy(p => p.Datum);
}
else
{
return listFilteredByDates.OrderBy(p => p.Datum);
}
}
}
else
{
if (loweredAndTrimmedLocations != null)
{
listFilteredByLocations = new List<Performance>();
listFilteredByLocations = performanceList.Where
(
performance =>
loweredAndTrimmedLocations.Any(location => location.Equals(performance.Location.Name.ToLower().Trim() + ", " + performance.Location.Stadt.ToLower().Trim()))
).ToList();
if (loweredAndTrimmedEvents != null)
{
listFilteredByEvents = new List<Performance>();
listFilteredByEvents = listFilteredByLocations.Where
(
performance =>
loweredAndTrimmedEvents.Any(evnt => evnt.Contains(performance.Event.Name.ToLower().Trim()))
).ToList();
return listFilteredByEvents.OrderBy(p => p.Datum);
}
else
{
return listFilteredByLocations.OrderBy(p => p.Datum);
}
}
else
{
if (loweredAndTrimmedEvents != null)
{
listFilteredByEvents = new List<Performance>();
listFilteredByEvents = performanceList.Where
(
performance =>
loweredAndTrimmedEvents.Any(evnt => evnt.Contains(performance.Event.Name.ToLower().Trim()))
).ToList();
return listFilteredByEvents.OrderBy(p => p.Datum);
}
else
{
return performanceList.OrderBy(p => p.Datum);
}
}
}
对于那些不想阅读该代码段的人: 我正在检查一个数组是否为空。如果没有,事件将被存储在数组中的选项过滤,然后我跳到下一个数组并检查它是否为空等等。每个步骤都使用先前过滤的事件列表。
我认为这不是一种优雅的方式。但我不知道如何做得更好。
你能否告诉我如何用这个代码生成更好,更精简和更易读的代码?