如何忽略远程搜索中的负数

时间:2016-12-20 18:00:37

标签: asp.net-mvc

我在MVC控制器中有以下if语句

                        if (vm.MileageLower > 0)
                    {
                        filter = new dtReport_v10_r1.QueueFilter();

                        filter.DataSource1_ID = 20;
                        filter.Formula1_ID = 0;
                        filter.ColumnName1 = "mileage";
                        filter.ColumnDesc1 = "mileage";
                        filter.Operator = ">";
                        filter.ValueList = vm.MileageLower.ToString();
                        queueObject.QueueFilters.Add(filter);
                        filter = null;
                    }

                    if (vm.MileageUpper > 0)
                    {
                        filter = new dtReport_v10_r1.QueueFilter();
                        filter.DataSource1_ID = 20;
                        filter.Formula1_ID = 0;
                        filter.ColumnName1 = "mileage";
                        filter.ColumnDesc1 = "mileage";
                        filter.Operator = "<=";
                        filter.ValueList = vm.MileageUpper.ToString();
                        queueObject.QueueFilters.Add(filter);
                        filter = null;
                    }

这里应该发生的是在mvc视图上产生远程搜索。当我运行视图时,它执行搜索没有任何问题,但是如果&#34; mileagelower&#34;值为零时,它会获得负数。

我意识到在现实世界的情况下,车辆不会有负里程,但是在搜索时需要删除测试值。

如何从搜索结果中消除负值?

只是了解更多信息,这是创建搜索值的地方

                oItem = new SelectListItem() { Value = "0", Text = "Any" };
            vm.MileageList.Add(oItem);
            for (var n = 0; n <= 14000; n += 1000)
            {
                oItem = new SelectListItem();
                oItem.Value = n.ToString();
                oItem.Text = n.ToString();
                vm.MileageList.Add(oItem);
            }
            for (var n = 15000; n <= 100000; n += 15000)
            {
                oItem = new SelectListItem();
                oItem.Value = n.ToString();
                oItem.Text = n.ToString();
                vm.MileageList.Add(oItem);
            }
            for (var n = 100000; n <= 200000; n += 25000)
            {
                oItem = new SelectListItem();
                oItem.Value = n.ToString();
                oItem.Text = n.ToString();
                vm.MileageList.Add(oItem);
            }
            for (var n = 225000; n <= 400000; n += 100000)
            {
                oItem = new SelectListItem();
                oItem.Value = n.ToString();
                oItem.Text = n.ToString();
                vm.MileageList.Add(oItem);
            }

1 个答案:

答案 0 :(得分:0)

您可以使用LINQ,这是我认为最合适的方式:

vm.MileageList.Where(x=>x.value > 0).ToList() should do the trick. 

或者你可以做一个&#34; foreach&#34;循环对象:

var list = new List<object>();
foreach(var nObject in vm.MilageList){
  if(nObject.Value < 0)continue;
  list.Add(nobject);
}