简体   繁体   English

如何过滤 IQueryable?

[英]How I can filter IQueryable?

I have the following controller:我有以下 controller:

public class EventsController : Controller
{
    private readonly ICustomerEventRepository _customerEventRepository;

    public EventsController(ICustomerEventRepository customerEventRepository)
    {
        _customerEventRepository = customerEventRepository;
    }

    public IActionResult Index(int? customerid, int? pageNumber, string code = "")
    {
        IQueryable<CustomerEvent> customerEvents;

        if (customerid.HasValue)
        {
            customerEvents =
                _customerEventRepository.CustomerEvents.Where(x => x.Customer.CustomerId == customerid)
                    .Include(x => x.Customer).OrderByDescending(x => x.CustomerEventId);
        }
        else if (!string.IsNullOrEmpty(code))
        {
            customerEvents =
                _customerEventRepository.CustomerEvents.Where(x => x.EventType == code)
                    .Include(x => x.Customer).OrderByDescending(x => x.CustomerEventId);
        }
        else
        {
            customerEvents = _customerEventRepository.CustomerEvents
                .Include(x => x.Customer).OrderByDescending(x => x.CustomerEventId);
        }

        var page = pageNumber ?? 1;

        var onePageOfEvents = customerEvents.ToPagedList(page, 15);

        return View(onePageOfEvents);
    }
}

Note that注意

_customerEventRepository = customerEventRepository;

is my repository.是我的存储库。 It returns IQueryable form my.它以我的形式返回IQueryable In the signature of the method Index I have a set of parameters.在方法Index的签名中,我有一组参数。 It's query parameters for filtering.它是用于过滤的查询参数。 Right now I have 2 ( pageNumber is not filter parameter, it's for pagination), but I'm planning more.现在我有 2 个( pageNumber不是过滤器参数,它用于分页),但我正在计划更多。 So this code is not very optimal.所以这段代码不是很优化。 If I have more filter parameters I will be forced to make more and more if instructions.如果我有更多的过滤器参数,我将被迫做出越来越多的if指令。 Because conditions don't work for LINQ .因为条件不适用于LINQ Maybe somebody had the same issue?也许有人有同样的问题? I appreciate any help.我很感激任何帮助。

If I have understood you correctly, you want to build up the query based on multiple conditions.如果我对您的理解正确,您希望根据多个条件构建查询。

IQueryable defers execution until the collection is materialised (for example calling ToList or iterating over the collection). IQueryable推迟执行直到集合实现(例如调用ToList或迭代集合)。

This allows you to build up the query piece by piece.这允许您逐个构建查询。 See an example below.请参阅下面的示例。

IQueryable<CustomerEvent> customerEvents = _customerEventRepository
                                               .CustomerEvents
                                               .Include(x => x.Customer);

if (customerid.HasValue)
{
    customerEvents = customerEvents.Where(x => x.Customer.CustomerId == customerid);
}

if (!string.IsNullOrEmpty(code))
{
    customerEvents = customerEvents.Where(x => x.EventType == code); 
}
// other conditions
...

// finally (this is when the query is actually executed)
var onePageOfEvents = customerEvents
                         .OrderByDescending(x => x.CustomerEventId)
                         .ToPagedList(page, 15);

return View(onePageOfEvents);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM