搜索多个条件?

时间:2016-08-11 06:24:04

标签: c# sorting search

我目前正在寻找一个网站,我遇到了一个问题,这似乎很难解决。我自己无法解决问题。

情况:在我搜索的当前状态下,我可以将事件排序为日期(例如,我可以选择 October ,它只会显示发生的事件10月),用于类别(每个事件都有类别)甚至搜索文本。现在当我选择十月作为一个月和节日作为一个类别时,我会在十月和所有节日中发生所有事件。我只想要10月份发生的节日。关于如何实现这一目标的任何想法?

这是我到目前为止所得到的:

var validEvents = new List<Item>();
var allEvents = ((LinkField)this.controlItem.Fields["Event Container"]).TargetItem.Children.ToList(); // getting the events

if (this.ddlMonths.SelectedIndex != 0 || this.ddlCategories.SelectedIndex != 0 || this.searchQuery.Text != string.Empty)
{
    foreach (var currentEvent in allEvents)
    {
        var isValid = false;

        // 1. Check for months
        if (this.ddlMonths.SelectedIndex != 0)
        {
            // .. validation if event should be displayed

            if (startDate <= monthDates[1].Date && endDate >= monthDates[0].Date)
            {
                isValid = true;
            }
        }

        // 2. Check for categories
        if (this.ddlCategories.SelectedIndex != 0)
        {
            foreach (var eventCategory in ((MultilistField) currentEvent.Fields["Categories"]).GetItems())
            {
                if (eventCategory["Category"].ToLower() == this.ddlCategories.SelectedValue.ToLower())
                {
                    isValid = true;
                }
            }
        }

        // 3. Check for search query
        if (this.searchQuery.Text != string.Empty)
        {
            var searchText = this.searchQuery.Text;

            if (currentEvent["Title"].Contains(searchText) || currentEvent["Text"].Contains(searchText))
            {
                isValid = true;
            }
        }

        if (isValid)
        {
            validEvents.Add(currentEvent);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我会把

false

在第8行,然后如果不满足任何条件,则将其设置为isValid。如果false已经var validEvents = new List<Item>(); var allEvents = ((LinkField)this.controlItem.Fields["Event Container"]).TargetItem.Children.ToList(); // getting the events if (this.ddlMonths.SelectedIndex != 0 || this.ddlCategories.SelectedIndex != 0 || this.searchQuery.Text != string.Empty) { foreach (var currentEvent in allEvents) { var isValid = true; // 1. Check for months if (this.ddlMonths.SelectedIndex != 0) { // .. validation if event should be displayed if (!(isValid && startDate <= monthDates[1].Date && endDate >= monthDates[0].Date)) { isValid = false; } } // 2. Check for categories if (this.ddlCategories.SelectedIndex != 0) { foreach (var eventCategory in ((MultilistField) currentEvent.Fields["Categories"]).GetItems()) { if (!(isValid && eventCategory["Category"].ToLower() == this.ddlCategories.SelectedValue.ToLower())) { isValid = false; } } } // 3. Check for search query if (this.searchQuery.Text != string.Empty) { var searchText = this.searchQuery.Text; if (!(isValid && currentEvent["Title"].Contains(searchText) || currentEvent["Text"].Contains(searchText))) { isValid = true; } } if (isValid) { validEvents.Add(currentEvent); } } } ,也请跳过以下检查。

希望它有所帮助!

编辑 - 完整代码:

all

答案 1 :(得分:0)

使用LINQ,您可以按如下方式组合搜索条件

var allEvents = ...;

IEnumerable<Item> events = allEvents;

if (ddlMonths.SelectedIndex != 0)
    events = events.Where(condition1);

if (ddlCategories.SelectedIndex != 0)
    events = events.Where(condition2);

if (searchQuery.Text != string.Empty)
    events = events.Where(condition3);

var validEvents = events.ToList();