0

I'm trying to get data in autocomplete from JsonResult Action Method in my controller. I cannot query data with LINQ, I'm sharing code please guide me.

Action Method

public class VehicleController : Controller
{
        private readonly IService<Vehicle> _service;

        public VehicleController(IService<Vehicle> service)
        {
            _service = service;
        }
    public JsonResult AutoComplete(string term)
    {
       var vehicle = _service.GetAll().Select(c => new { Name = c.Name });
       return Json(vehicle, JsonRequestBehavior.AllowGet);
    }
}

EntityRepository

public class EntityRepository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
{
    private readonly IEntitiesContext _context;
    private readonly IDbSet<TEntity> _dbEntitySet;
    private bool _disposed;

    public EntityRepository(IEntitiesContext context)
    {
       _context = context;
       _dbEntitySet = _context.Set<TEntity>();
    }

    public List<TEntity> GetAll()
    {
       return _dbEntitySet.ToList();
    }
}

IRepository

public interface IRepository<TEntity> : IDisposable where TEntity : BaseEntity
{
   List<TEntity> GetAll();
}

Service

public class Service<TEntity> : IService<TEntity> where TEntity : BaseEntity
{
   public IUnitOfWork UnitOfWork { get; private set; }
   private readonly IRepository<TEntity> _repository;
   private bool _disposed;

   public Service(IUnitOfWork unitOfWork)
   {
      UnitOfWork = unitOfWork;
      _repository = UnitOfWork.Repository<TEntity>();
   }

   public List<TEntity> GetAll()
   {
      return _repository.GetAll();
   }
}

IService

public interface IService<TEntity> : IService where TEntity : BaseEntity
{
    List<TEntity> GetAll();
}

Action Method is returning a complete list of data but when I apply any filter it won't work and returns no values -- e.g I tried this and it returns null:

var vehicle = _service.GetAll().Where(c => c.Name.StartsWith(term)).Select(c => new { Name = c.Name });
14
  • Sorry but where you pass term parameter from action to service method? Commented Jun 16, 2015 at 8:11
  • @Kamo it is an autocomplete, term is coming from UI and in service, how i can pass? Commented Jun 16, 2015 at 8:14
  • 1
    It's ok, now I see how you used it :) Are you sure term matches any result in your DB? Remember this comparison is case-sensitive(so 'm' doesn't match 'M') Commented Jun 16, 2015 at 8:21
  • @freedomn-m - I cannot agree - author uses ToList() method before Where() so further comparison is not the part of LinqToSql Commented Jun 16, 2015 at 8:30
  • You've not shown the View (first line would suffice). Can you also show the code where it does work (with just Getall() is implied in the QA, but not clear) .GetAll() returns a Vehicle enumeration, but .GetAll().Where...Select(c=>new { Name... }) returns a string enumeration. Commented Jun 16, 2015 at 8:31

1 Answer 1

1

Ensure term matches the case of the data. As all the data is loaded (.ToList() in the DAL), the .Where clause uses .Net comparison rather than SQL comparison:

    var vehicle = _service.GetAll().Where(c => c.Name.StartsWith(term, StringComparison.OrdinalIgnoreCase)...

If, in the future, you want to change this to Contains, you can add an extension method:

    public static bool Contains(this string source, string toCheck, StringComparison comp)
    {
        if (source == null) throw new ArgumentNullException("source");
        return source.IndexOf(toCheck, comp) >= 0;
    }

then

    var vehicle = _service.GetAll().Where(c => c.Name.Contains(term, StringComparison.OrdinalIgnoreCase)...
Sign up to request clarification or add additional context in comments.

2 Comments

One more things, can i apply select in resultant list? currently it is returning all fields i just want to return only Name field, can i do this with this solution?\
Of course, now you've confirmed the issue, you can change the return type to just string and change the code that handles it to just string using the Select you had in the original question.

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.