I am trying to build the internet-shop using MVC3. I am using EF and I have one interface to work with my entity context:
public interface IBaseRepository<T> where T : class, IBase, new()
{
IQueryable<T> Get();
}
and one abstract class to work with this interface:
public abstract class BaseRepository<T> :
IBaseRepository<T> where T : class, IBase, new()
{
protected abstract ObjectSet<T> EntitySet { get; }
public virtual IQueryable<T> Get()
{
return from obj in EntitySet select obj;
}
}
So I can use this solution to work with different tables in my database(SQL). I can’t post images yet. So I will try to describe my database:
[Product] – [CategoryProduct(ProductID,CategoryID)] – [Category(ShortName)]
- Table Product
- Table Category with field ShortName
- Table CategoryProduct with fields ProductID,CategoryID to make many-to-many link.
(We are getting closer to the problem….)
I have a method, which take “ShortName” from Table “Category”(“repository” is the dbcontext only for one table – “Category”) And I need to return IEnumerable<> of Products in my view:
public ActionResult GetProductInCategory(string shortName)
{
IEnumerable<Product> = repository.Get()......
return View();
}
So here is the problem: I need to make a query to take product from these 3 tables using lambda, but i can’t understand, how to do this.
I can’t use linq, because I have direct access only to one table in the whole database.
Sorry for bad eplanation, if you have any questions about architecture to get the whole view, I will try to answer you.
I’m assuming/guessing from your use of the
EntitySetyou’re using EntityFramework and you have this structure in you Entity ClassesSo you could do this in your linq query: