c# - How to read iCollection data in repository pattern - ASP.NET- MVC Entity Framework -
i using repository pattern , unit of work in asp.net-mvc application. have generic repository crud operation , service class between unit of work , controller class; meaning controller class call unit of work access operation great encapsulate data access , business logic web application.
now question how can icollection data within unit of work. taking example below
student model
public partial class student { public student() { this.studentcourses = new hashset<studentcourse>(); } public int studentid { get; set; } public string name { get; set; } public virtual icollection<studentcourse> studentcourses { get; set; } }
course model
public partial class course { public course() { this.studentcourses = new hashset<studentcourse>(); } public int courseid { get; set; } public string title { get; set; } public virtual icollection<studentcourse> studentcourses { get; set; } }
studentcourse model
public partial class studentcourse { [key] public int studentcourseid { get; set; } [key] [foreignkey("student")] public int studentid { get; set; } [key] [foreignkey("course")] public int courseid { get; set; } public virtual course course { get; set; } public virtual student student { get; set; } }
how can achieve following linq query result using repository pattern , unit of work. struggling because generic repository classes give students record not courses unless missing here. using data annotation not fluent api
using (var db2 = new mydbcontext()) { list<student> _studentread = new list<student>(); _studentread = (from _student in db2.students .include(r => r.studentcourses.select(sc => sc.course)) select _student).tolist(); }
generic repository interface
public interface igenericrepository<tentity> tentity :class { global::system.linq.iqueryable<tentity> getall(); tentity getentitybyid(int id); void insertentity(tentity obj); void updateentity(tentity obj); void deleteentity(int id); }
generic repository
public class genericrepository<tentity> : igenericrepository<tentity> tentity : class { protected dbset<tentity> _dbset; private readonly dbcontext _dbcontext; public genericrepository() { } public genericrepository(dbcontext dbcontext) { this._dbcontext = dbcontext; _dbset = _dbcontext.set<tentity>(); } public iqueryable<tentity> getall() { return _dbset; } public tentity getentitybyid(int id) { tentity obj = _dbset.find(id); return obj; } public void insertentity(tentity obj) { _dbset.add(obj); } public void updateentity(tentity obj) { _dbcontext.entry(obj).state = entitystate.modified; } public void deleteentity(int id) { tentity obj = _dbset.find(id); _dbset.remove(obj); } }
to courses following:
var repository = new genericrepository<course>(); var courses = repository.getall();
to use linq on courses can do:
var courses = c in repository.getall() select c;
or
var courses = c in repository.getall() c.studentcourses.studentid == 1234 select c;
or even:
var courses = repository.getall(); var studentcourses = c in courses c.studentcourses.studentid == 1234 select c;
which same as:
var courses = repository.getall(); var studentcourses = courses.where(x => x.studentcourses.studentid == 1234);
hope helps bit, if not comment , more specific you;re trying achieve.
Comments
Post a Comment