简体   繁体   English

只能使用可选参数进行查询

[英]IQueryable with only optional parameters

I currently have the following method: 我目前有以下方法:

        public List<Order> GetOrders(int profileId, string timeSpan, string workOrd, string partNo, bool includeDeleted)
    {
        DateTime startDate = DateTime.Now;
        DateTime endDate = DateTime.Now;
        string[] times = (!string.IsNullOrWhiteSpace(timeSpan)) ? timeSpan.Trim().Split('-') : new string[] { "", "" };
        if (!string.IsNullOrWhiteSpace(times[0]) && !string.IsNullOrWhiteSpace(times[0]))
        {
            startDate = DateTime.Parse(times[0]).Date;
            endDate = DateTime.Parse(times[1]).Date;
        }

        //New Real Query
        IQueryable<Order_Travel> otQuery = _context.Order_Travels.Where(x =>
            (profileId != 0 || x.Profile.ProfileID == profileId)
            && ((timeSpan == null || timeSpan.Trim() == "") || ((DbFunctions.TruncateTime(x.TimeRecieved) >= startDate) 
                                                            && (DbFunctions.TruncateTime(x.TimeRecieved) <= endDate)))
            && ((workOrd == null || workOrd.Trim() == "") || x.Order.WorkOrdNo == workOrd)
            && ((partNo == null ||partNo.Trim() == "") || x.Order.PartNo == partNo)
            && (!includeDeleted || x.Aborted == true));

        //The results is now in order_travel. Under here binding them to a list of orders with only the respective orderTravels included.
        List<Order> orders = new List<Order>();
        List<Order_Travel> ots = otQuery.ToList();
        foreach (Order_Travel ot in ots)
        {
            var OrderInList = orders.FirstOrDefault(X => X == ot.Order);
            if (OrderInList == null)
            {
                orders.Add(ot.Order);
                OrderInList = orders.FirstOrDefault(X => X == ot.Order);
                OrderInList.OrderTravels.Clear();
                OrderInList.OrderTravels.Add(ot);
            }
            else
            {
                OrderInList.OrderTravels.Add(ot);
            }
        }
        return orders;
    }

What I need it to do, is (as I've attempted) to make a call, finding all Order_Travel objects that match the paramters sent to it. 我需要做的是(按照我的尝试)打电话,找到与发送给它的参数匹配的所有Order_Travel对象。 If some (or all) are left blank, it takes everything, regardless of the values. 如果某些(或全部)留空,则无论值如何,它都会占用所有内容。 The code right now, does not return anything, if a blank search is made (a search that does not have any parameters), and I can not see what could be the issue. 如果进行空白搜索(没有任何参数的搜索),那么现在的代码将不返回任何内容,并且我看不到可能是什么问题。 I have tried debugging it, but with no luck. 我已经尝试调试它,但是没有运气。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Thanks! 谢谢!

Filter one option at a time, instead of trying to put everything into a single expression: 一次过滤一个选项,而不是尝试将所有内容放入一个表达式中:

IQueryable<T> query = all;    // start with everything

if (IsPresent(option1))
{
   query = query.Where(t => t.XXX == option1);   
}

Example

IQueryable<Order_Travel> otQuery = _context.Order_Travels;

if (profileId != 0)
{
    otQuery = otQuery.Where(x => x.Profile.ProfileID == profileId);
}

if (timeSpan != null && timeSpan.Trim() != "")
{
    otQuery = otQuery.Where(x => DbFunctions.TruncateTime(x.TimeRecieved) >= startDate &&
                                 DbFunctions.TruncateTime(x.TimeRecieved) <= endDate);
}

You will also find this easier to maintain than one huge expression. 您还会发现,这比一个庞大的表达式更容易维护。

Probably this part is your problem: 可能这是您的问题:

(profileId != 0 || x.Profile.ProfileID == profileId)

It should be 它应该是

(profileId == 0 || x.Profile.ProfileID == profileId)

If your profile ID is 0, it will only find entries with x.Profile.ProfileID being 0 . 如果您的个人资料ID为0,则只会找到x.Profile.ProfileID0条目。 Probably there are no such entries. 可能没有此类条目。

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

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