0

how to use following function

Generic Function:

public T GetSingle(Expression<Func<T, bool>> whereCondition)
       {
           return this.ObjectSet.Where(whereCondition).FirstOrDefault<>();
       }

Business logic wise:

//Now in the following function i would like to call Generic function.

public TabMasterViewModel GetSingle(Expression<Func<TabMasterViewModel, bool>> whereCondition)
       {

           _tabmasterRepository.GetSingle( .. what should be here.. );
       }

//Calling function from Controller level.

public ActionResult Details(int id)
       {
           return View(_tabmasterService.GetSingle(x => x.colID == id));
       }

I could not able to use the function, please suggest.

_tabmasterRepository.GetSingle( .. what should be here.. );

Thanks, Imdadhusen

5
  • i want to return TabMasterViewModel type record Commented Jun 4, 2011 at 9:24
  • possible duplicate of How to execute function? which type of paramter should? Commented Jun 4, 2011 at 9:31
  • yes i have implemented code as suggested by you at the Controller level return View(_tabmasterService.GetSingle(x => x.colID == id)); but the i also have to pass the GetSingle to the Generic Function Commented Jun 4, 2011 at 9:44
  • maybe i have misunderstood then, i will delete my comment Commented Jun 4, 2011 at 10:08
  • the follow is like calling function from Controller >> Base >> Generic. and i have problem in Base Commented Jun 4, 2011 at 10:31

1 Answer 1

1

Either you modify your first generic function as

public T GetSingle(Expression<Func<T, bool>> whereCondition)
{
     return context.CreateObjectSet<T>().Where(whereCondition).FirstOrDefault();
}

or create a genetic repository

public class RepositoryGeneric<TEntity>
{
    public RepositoryGeneric(Context context)
    {
        Context = context;         
    }

    protected ObjectContext Context { get; private set; }

    protected virtual ObjectSet<TEntity> ObjectSet
    {
        get { return Context.CreateObjectSet<TEntity>(); }
    }

    public virtual TEntity GetByKey(params object[] keys)
    {
        return DbSet.Find(keys);
    }

    public TEntity GetSingle(Expression<Func<TEntity, bool>> whereCondition)
    {
        return ObjectSet.Where(whereCondition).FirstOrDefault();
    }
}

Edit:

using the generic function

TabMasterViewModel model = _tabmasterService.GetSingle(x => x.colID == id);

or using generic repository

var tabmasterRepository = new RepositoryGeneric<TabMasterViewModel>(new Context());
var model = tabmasterRepository.GetSingle(x => x.colID == id);
Sign up to request clarification or add additional context in comments.

2 Comments

edited it to add an example. is entity type different from TabMasterViewModel?
No it same for TabMasterViewModel

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.