linq到实体不能将int转换为字符串 [英] linq to entities can not convert int to string

查看:115
本文介绍了linq到实体不能将int转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过滤返回剑道组合框的数据,该过滤基于ID, 我需要返回所有包含过滤文本的记录,这些记录不仅等于1,所以我要做的就是将ID转换为字符串,如下例所示

I try to filtering the data that return to kendo combo box, the filtering is based on the ID, I need to return all records that contains the filtration text not only the equals one, so what I did is to cast the ID to string as the following snip

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

但它总是返回 对实体的linq无法识别方法'system.string tostring()'

but it's always return linq to entities does not recognize the method 'system.string tostring()'

所以我尝试将dbset转换为清单,在另一篇文章中找到的where语句之前 LINQ to Entities无法识别该方法MVC 4中的"System.String ToString()"方法 但是我又收到一个错误,说该列表不包含Where的定义(dbSet是IDbSet的一个实例)

so I tried to convert the dbset to list before the where statement as I found in another post LINQ to Entities does not recognize the method 'System.String ToString()' method in MVC 4 but I got another error says that the list does not contains a definition for Where (dbSet is an instance of IDbSet)

public virtual IList<T> GetMany(Expression<Func<T, bool>> where)
{

    return dbset.ToList().Where(where).ToList();
}

这是我原来的(当前)获取方法

here is My original(current) get method

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data;
using System.Linq.Expressions;
using Spine.ERP.DataAccess.DbFirstDataAccess;
using Spine.ERP.DataModel.Helpers;
namespace Spine.ERP.DataAccess.Infrastructure
{
    public abstract class RepositoryBase<T> where T : class
    {

        private SSSDBEntities dataContext;

        private readonly IDbSet<T> dbset;

        protected RepositoryBase(IDatabaseFactory databaseFactory)
        {
            DatabaseFactory = databaseFactory;
            dbset = DataContext.Set<T>();
        }

       protected IDatabaseFactory DatabaseFactory
        {
            get;
            private set;
        }


        protected SSSDBEntities DataContext
        {
            get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
        }
    public virtual IQueryable<T> GetMany(Expression<Func<T, bool>> where)
        {
            return dbset.Where(where);
        }
  }
}

我还能做其他事情来解决这个问题吗?

Any other thing can I do to solve this problem?

推荐答案

有一个

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))

这篇关于linq到实体不能将int转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆