首页 新闻 搜索 专区 学院

如何对字符串进行区间查询?

-1
悬赏园豆:20 [待解决问题]

documentNumber为string类型:

数据为:

201509001

201509002

201509003

201511001

201511002

201511003

201511004

.....

如何查询201509003-201511002之间的记录。

我的想法是通过转为数值来处理,做法如下:

            var result = (from a in db_OA.VehicleMaintenanceRecords
                          join b in db_OA.VehicleDatas on a.licensePlate equals b.licensePlate
                          join c in db_OA.PayoffTables on a.documentNumber equals c.documentNumber
                          into Grp
                          from grp in Grp.DefaultIfEmpty()
                          where (grp.receiptDate == null || a.leaveFactoryDate == null)
                          orderby a.documentNumber descending
                          select new
                          {
                              documentNumber = int.Parse(a.documentNumber),//单号为:string类型
                              licensePlate = b.licensePlate,//车牌号
                              vehicleOwner = b.vehicleOwner,//车主
                              registrationDate = a.registrationDate,//登记(进厂)日期
                              twoLevelMaintenance = a.twoLevelMaintenance,//二保
                              maintenanceProject = a.maintenanceProject,//检查项目
                              materialCost = db_OA.OutRecordTBs.Where(n => n.documentNumber == a.documentNumber).Sum(n => n.sellingPrice.HasValue ? n.sellingPrice * n.outNumber : 0),//配件金额
                              settlementMark = db_OA.PayoffTables.Any(n => n.documentNumber == a.documentNumber),//结算
                              manHourCost = db_OA.PayoffTables.FirstOrDefault(n => n.documentNumber == a.documentNumber).manHourCost,//工时费
                              leaveFactoryDate = a.leaveFactoryDate,//出厂日期
                              collectionAmount = grp.collectionAmount//收款金额
                          }).ToList();


            var orderList = result.Where(p => p.documentNumber >= s_documentNumber && p.documentNumber >= e_documentNumber);


错误提示为:

LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression.

请高人指点。谢谢

我的QQ:191971159

润物之音的主页 润物之音 | 初学一级 | 园豆:101
提问于:2015-10-30 13:16
< >
分享
所有回答(1)
0

不要在Select里转换. 在下面转换. 

var orderList = result.Where(p => int.Parse(p.documentNumber) >= s_documentNumber && int.Parse(p.documentNumber) >= e_documentNumber);

 

李丶GuanYao | 园豆:1228 (小虾三级) | 2015-10-30 14:52

谢谢!但还是有问题,当s_documentNumber或e_documentNumber为0则不以这为条件查询。我写的代码如下:

(是参考:http://www.cnblogs.com/still-windows7/p/3468355.html)

            int s_documentNumber = Request.Form["s_documentNumber"] == "" ? 0 : int.Parse(Request.Form["s_documentNumber"]);
            int e_documentNumber = Request.Form["e_documentNumber"] == "" ? 0 : int.Parse(Request.Form["e_documentNumber"]);
查询语句为:

var orderList = result.Where(p => s_documentNumber == 0 ? true : int.Parse(p.documentNumber) >= s_documentNumber
                && e_documentNumber == 0 ? true : int.Parse(p.documentNumber) <= e_documentNumber);

其结果非为所要的为0则不以这为条件查询。

支持(0) 反对(0) 润物之音 | 园豆:101 (初学一级) | 2015-10-30 15:33

@润物之音: 

加上括号试试。

var orderList = result.Where(p => (s_documentNumber == 0 ? true : int.Parse(p.documentNumber) >= s_documentNumber)
                && (e_documentNumber == 0 ? true : int.Parse(p.documentNumber) <= e_documentNumber));

支持(0) 反对(0) 李丶GuanYao | 园豆:1228 (小虾三级) | 2015-10-30 16:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册