且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

linq到实体不能将int转换为字符串

更新时间:2022-05-18 04:49:19

有一个

There is a SqlFunctions class available that contains many SQL-functions that may be used for LINQ. Try a look at SqlFunctions.StringConvert

Items = Mapper.Map<List<PurchaseOrder>, List<PurchaseOrderViewModel>>(
                purchaseOrderRepository
                   .GetMany(x => SqlFunctions
                                  .StringConvert((double?) x.PurchaseOrderID)
                                  .Contains(text))
                                  .ToList());

很遗憾,只有SqlFunctions.StringConvert(double? number),没有SqlFunctions.StringConvert(int? number).但是我总是将整数转换为双精度,并且可以按预期工作.

Unfortunately there is only a SqlFunctions.StringConvert(double? number) and no SqlFunctions.StringConvert(int? number). But I always convert the integer number to a double and it works as expected.

您正在呼叫ToList之前的Where.因此,在ToList()是非法的之后,只能在LINQ to Entity query中的任何SqlFunction中调用SqlFunction.

You are calling ToList prior Where. Therefore that SqlFunctions can only be called in a LINQ to Entity query any SqlFunctions after a ToList() is illegal.

尝试不使用GetMany方法:

Try without your GetMany Method:

purchaseOrderRepository.Set<PurchaseOrder>()
   .Where(x => SqlFunctions
       .StringConvert((double?) x.PurchaseOrderID)
       .Contains(text))