如何在LINQ查询中应用多个可选过滤器

时间:2018-07-11 07:32:57

标签: c# asp.net-mvc linq iqueryable custom-model-binder

我有一个页面,用户可以在其中选择任意数量的搜索过滤器以应用搜索

enter image description here

当用户单击搜索时,这些参数将传递到我的GetIndicatorData方法中以执行查询。但是,它似乎对我不起作用。

这是我的代码

public static List<tblindicators_data_custom> GetIndicatorsData(string status, int service_id, int operator_id, string year, string frequency)
{


    var date = Convert.ToDateTime(year + "-01-01");
    int[] numbers = status.Split(',').Select(n => int.Parse(n)).ToArray();
    var ict = new ICT_indicatorsEntities();
    var result = from ind in ict.tblindicators_data
                 join ser in ict.tblservices on ind.service_id equals ser.Id
                 join oper in ict.tbloperators on ind.operator_id equals oper.Id
                 where numbers.Contains(ind.status) && (ind.date_added.Year == date.Year) 
                 select new
                 {
                     ind.Id,
                     ind.service_id,
                     ind.survey_id,
                     ind.operator_id,
                     ind.type,
                     ind.date_added,
                     ind.quater_start,
                     ind.quater_end,
                     ind.status,
                     ind.month,
                     service = ser.name,
                     operator_name = oper.name
                 };

    List<tblindicators_data_custom> data = new List<tblindicators_data_custom>();
    foreach (var item in result)
    {
        tblindicators_data_custom row = new tblindicators_data_custom();
        row.Id = item.Id;
        row.survey_id = item.survey_id;
        row.service_id = item.service_id;
        row.service_name = item.service;
        row.operator_id = item.operator_id;
        row.operator_name = item.operator_name;
        row.date_added = item.date_added;
        row.quater_start = item.quater_start;
        row.type = item.type;
        row.quater_end = item.quater_end;
        row.month = item.month == null? DateTime.Now:item.month;
        row.status = item.status;
        data.Add(row);
    }

    return data;
}

0 个答案:

没有答案