2

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

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

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?

1 Answer 1

10

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

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.

Edit: 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.

Try without your GetMany Method:

purchaseOrderRepository.Set<PurchaseOrder>()
   .Where(x => SqlFunctions
       .StringConvert((double?) x.PurchaseOrderID)
       .Contains(text))
Sign up to request clarification or add additional context in comments.

26 Comments

Thanks for your reply but I try this solution before but the same error return
Stange, it should work. I have never had a problem with that. I cannot see the reason why with this code the same error should arise. Are you somewhere calling ToString()
No I didn't call ToString() anywhere
I cannot understand why you get exactly the same error. At least it should be different. (I have edited the above code: not the cast ist to double? instead of double; maybe thats the problem)
Thank for your help, but it's still raise the same error maybe it should convert to List first then filtering, but I can not do that because it's always return that the where not found
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.