0

i have a generic repository

    public TEntity GetByCondition(Func<TEntity, Boolean> where)
    {
        return DbSet.Where(where).FirstOrDefault<TEntity>();
    }

i need to get the selected records based on condition;

    public IEnumerable<ResultEntity> GetResultByParams(string _RollNo, string _Class)
    {
      var result = _unitOfWork.ResultRepository.GetByCondition(); // i need to check ondition of _RollNo here. Please guide me the usage. 
    }

// how to use the var result = _unitOfWork.ResultRepository.GetByCondition(); with condition

1 Answers1

0

In general:

var result = _unitOfWork.ResultRepository.GetByCondition(res => /*your condition for "result" here*/);

By condition I mean expression that returns bool.

For example:

var result = _unitOfWork.ResultRepository.GetByCondition(res => res.RollNo == _RollNo);

It basically means: "give me all Results which have RollNo equals to _RollNo"

I recommend you to read explanations of Func from following question, it may be useful: Explanation of Func.

And, of course, msdn reference for the type of Func-s used in your case.

  • I need to have record in return. not true false – Rahib Memon Aug 02 '17 at 16:29
  • Error 3 Could not find an implementation of the query pattern for source type 'DataModel.tbl_Result'. 'Where' not found. E:\Dump\taskapi\api\BusinessServices\ResultService\ResultService.cs 41 45 BusinessServices – Rahib Memon Aug 02 '17 at 16:31
  • My answer is not implying that you'll receive a bool instead of entity. I just show how to use your method `GetByCondition` using lambda expression. – Pavel Aschepkov Aug 02 '17 at 16:39
  • This error is not seems to be related to any of I wrote in answer. If you'll provide more context (for example, line of code where error occurs), i'll try to help. – Pavel Aschepkov Aug 02 '17 at 16:41
  • Error 3 Could not find an implementation of the query pattern for source type 'DataModel.tbl_Result'. 'Where' not found – Rahib Memon Aug 02 '17 at 16:41
  • Please guide me, I am using RepositoryPattern first time – Rahib Memon Aug 02 '17 at 16:41
  • if (result != null) { Mapper.CreateMap(); var resultInfo = from rs in result // here getting error where rs.RollNo == _RollNo && rs.Class == _Class orderby rs.ID descending select rs; var ResultModel = Mapper.Map, List>(resultInfo.ToList()); return ResultModel; } return null; – Rahib Memon Aug 02 '17 at 16:46
  • OK, let's clarify: you have GetByCondition method of your repository and you have no idea what to pass as argument, right? Well, if so, I just showed in answer what you should pass there: it's lambda expression, which body should return bool (specifying a condition for your entities). – Pavel Aschepkov Aug 02 '17 at 16:49
  • As for error, it's probably conflict of names. Try to rename `result` from my example to anything else, for example to `entity` and see if error persists. – Pavel Aschepkov Aug 02 '17 at 16:50
  • I changed but the same – Rahib Memon Aug 02 '17 at 16:55
  • can I use direct query to entity like; – Rahib Memon Aug 02 '17 at 16:55
  • var context = new taskAPIDBEntities(); var query = context.tbl_Result.Where(x => x.RollNo == _RollNo).Single(); – Rahib Memon Aug 02 '17 at 16:55
  • If it is not the conflict, and `result` is meant to be the result of GetByCondition then the problem is that you trying to apply LINQ to SQL language syntax (`from rs in result...`) to something that has not `Where` method. In this case it is ResultEntity which obviously has not such method (it would make sense if GetByCondition returned IQueryable of IEnumerable, but it returning a single entity). – Pavel Aschepkov Aug 02 '17 at 16:57
  • Yes, you can, but doing this you're definetely not using repository pattern. – Pavel Aschepkov Aug 02 '17 at 16:58
  • And, by the way, if this is the DbContext, you should use `using` or at least dispose your `context` later, when you're done. – Pavel Aschepkov Aug 02 '17 at 16:59
  • like this: using(var context = new taskAPIDBEntities()) { /*do you staff here, it'll automatically dispose since you'll leave this code block*/ } – Pavel Aschepkov Aug 02 '17 at 17:00
  • yes, again giving the error: 'System.Data.Entity.DbSet`1' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Data.Entity.DbSet`1' could be found (are you missing a using directive or an assembly reference?) – Rahib Memon Aug 02 '17 at 17:02
  • Do you have `using System.Linq` in the file? `DbSet` is `DbQuery` which implements IQueryable, so `Where` here should be an extension method `System.Linq.Queryable.Where`. – Pavel Aschepkov Aug 02 '17 at 17:06
  • '' – Rahib Memon Aug 03 '17 at 12:37
  • trying to pass value from selected item to Request – Rahib Memon Aug 03 '17 at 12:37