从数组添加匹配到列表

时间:2016-12-25 13:36:03

标签: c# asp.net search model-view-controller ienumerable

我目前在搜索结果方面遇到问题。用户可以搜索不同的项目,例如类别,类型,价格等。问题是当我选择多个类别时,我返回的列表没有返回项目。 我有一个通过ajax提交GET请求的视图,并且数据被正确加载到mvc action方法中,该方法将传递给返回匹配结果的my方法。这些类别作为数组发送到viewModel中。

public IEnumerable<AdSearchViewModel> GetItemsFromSearch(AdSearchViewModel viewModel)
{
    var result = from a in _dbContext.AdTable
                    select new AdSearchViewModel
                    {
                        Id = a.Id,
                        Name = a.Name,
                        CategoryId = a.CategoryId,
                        TypeId = a.TypeId,
                        Brand = a.Brand,
                        Price = a.Price,
                        Description = a.Description
                    };
    if (viewModel != null)
    {
        if (viewModel.Id.HasValue)
            result = result.Where(x => x.Id == viewModel.Id);
        if (viewModel.SelectedCategory != null && viewModel.SelectedCategory.Length > 0)
        {
            result = viewModel.SelectedCategory.Aggregate(result, (current, item) => current.Where(x => x.CategoryId == item));
        }
        if (!string.IsNullOrEmpty(viewModel.Name))
            result = result.Where(x => x.Name.Contains(viewModel.Name));
        if (!string.IsNullOrEmpty(viewModel.Brand))
            result = result.Where(x => x.Brand.Contains(viewModel.Brand));
        if (viewModel.PriceFrom.HasValue)
            result = result.Where(x => x.Price >= viewModel.PriceFrom);
        if (viewModel.PriceFrom.HasValue)
            result = result.Where(x => x.Price <= viewModel.PriceTo);
    }
    //TODO: return newest 
    return result;
}

public IEnumerable<AdSearchViewModel> GetItemsFromSearch(AdSearchViewModel viewModel) { var result = from a in _dbContext.AdTable select new AdSearchViewModel { Id = a.Id, Name = a.Name, CategoryId = a.CategoryId, TypeId = a.TypeId, Brand = a.Brand, Price = a.Price, Description = a.Description }; if (viewModel != null) { if (viewModel.Id.HasValue) result = result.Where(x => x.Id == viewModel.Id); if (viewModel.SelectedCategory != null && viewModel.SelectedCategory.Length > 0) { result = viewModel.SelectedCategory.Aggregate(result, (current, item) => current.Where(x => x.CategoryId == item)); } if (!string.IsNullOrEmpty(viewModel.Name)) result = result.Where(x => x.Name.Contains(viewModel.Name)); if (!string.IsNullOrEmpty(viewModel.Brand)) result = result.Where(x => x.Brand.Contains(viewModel.Brand)); if (viewModel.PriceFrom.HasValue) result = result.Where(x => x.Price >= viewModel.PriceFrom); if (viewModel.PriceFrom.HasValue) result = result.Where(x => x.Price <= viewModel.PriceTo); } //TODO: return newest return result; } 当我只点击一个类别时,会加载正确的项目。我尝试使用数组的foreach(仍然没有运气),但Visual Studio建议调用错误的聚合函数({“方法只能在Type.IsGenericParameter为真的类型上调用。”})。

在IsGenericParameter属性为false的类型上获取DeclaringMethod属性会引发InvalidOperationException。 - MSND文档

使用所选类别添加匹配结果的正确方法是什么?

我找到了解决方案! 这对我有用:

1 个答案:

答案 0 :(得分:1)

Chnage将您的操作返回值:

public IEnumerable<AdSearchViewModel> GetItemsFromSearch(AdSearchViewModel viewModel)