PDA

View Full Version : join کردن دو ریپوزیتوری با هم



sara_t
یک شنبه 21 آذر 1395, 11:55 صبح
سلام دوستان

دوستان من برای هر جدولم یک repository نوشتم. الان احتیاج دارم دوتا جدولم رو با هم join کنم. چظور میتونم دوتا repository رو با هم جوین کنم:

در واقع یه همچین جوینی رو میخام:


select product.name from product inner join groups on product.groupid=group.id where group.parentid=1

حالا من در کنترلر اکشنم بصورت زیر هست:

public ActionResult Product(int GroupId)
{
GroupRepository blGroup = new GroupRepository();
ProductRepository blProduct = new ProductRepository();
var model = new MVCEshop.ViewModels.Home.ProductViewModel();}

چطور میتنوم این دوتا ریپوزیتوری رو بهم وصل کنم

----------------------------------------------------------------
اینم از ریپوزیتاریا:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace MVCEshop.Models.Repositories
{
public class GroupRepository : IDisposable
{
private MVCEshop.Models.DomainModels.EshopEntities db = null;


public GroupRepository()
{
db = new DomainModels.EshopEntities();
}


public bool Add(MVCEshop.Models.DomainModels.Group entity, bool autoSave = true)
{
try
{
db.Groups.Add(entity);
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
else
return false;
}
catch
{
return false;
}
}


public bool Update(MVCEshop.Models.DomainModels.Group entity, bool autoSave = true)
{
try
{
db.Groups.Attach(entity);
db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
else
return false;
}
catch
{
return false;
}
}


public bool Delete(MVCEshop.Models.DomainModels.Group entity, bool autoSave = true)
{
try
{
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
else
return false;
}
catch
{
return false;
}
}


public bool Delete(int id, bool autoSave = true)
{
try
{
var entity = db.Groups.Find(id);
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
else
return false;
}
catch
{
return false;
}
}


public MVCEshop.Models.DomainModels.Group Find(int id)
{
try
{
return db.Groups.Find(id);
}
catch
{
return null;
}
}


public IQueryable<MVCEshop.Models.DomainModels.Group> Where(System.Linq.Expressions.Expression<Func<MVCEshop.Models.DomainModels.Group, bool>> predicate)
{
try
{
return db.Groups.Where(predicate);
}
catch
{
return null;
}
}


public IQueryable<MVCEshop.Models.DomainModels.Group> Select()
{
try
{
return db.Groups.AsQueryable();
}
catch
{
return null;
}
}


public IQueryable<TResult> Select<TResult>(System.Linq.Expressions.Expression<Func<MVCEshop.Models.DomainModels.Group, TResult>> selector)
{
try
{
return db.Groups.Select(selector);
}
catch
{
return null;
}
}


public int GetLastIdentity()
{
try
{
if (db.Groups.Any())
return db.Groups.OrderByDescending(p => p.Id).First().Id;
else
return 0;
}
catch
{
return -1;
}
}


public int Save()
{
try
{
return db.SaveChanges();
}
catch
{
return -1;
}
}


public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}


protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this.db != null)
{
this.db.Dispose();
this.db = null;
}
}
}


~GroupRepository()
{
Dispose(false);
}
}
}


و ریپوزیتوری product:


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;


namespace MVCEshop.Models.Repositories
{
public class ProductRepository : IDisposable
{
private MVCEshop.Models.DomainModels.EshopEntities db = null;


public ProductRepository()
{
db = new DomainModels.EshopEntities();
}


public bool Add(MVCEshop.Models.DomainModels.Product entity, bool autoSave = true)
{
try
{
db.Products.Add(entity);
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
else
return false;
}
catch
{
return false;
}
}


public bool Update(MVCEshop.Models.DomainModels.Product entity, string imagePath, bool autoSave = true)
{
try
{
if (imagePath != null)
{
entity.Image = imagePath;
}
db.Products.Attach(entity);
db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
else
return false;
}
catch
{
return false;
}
}


public bool Delete(MVCEshop.Models.DomainModels.Product entity, bool autoSave = true)
{
try
{
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
else
return false;
}
catch
{
return false;
}
}


public bool Delete(int id, bool autoSave = true)
{
try
{
var entity = db.Products.Find(id);
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
{
bool result = Convert.ToBoolean(db.SaveChanges());
if (result)
{
try
{
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\Files\\UploadImages\\" + entity.Image) == true)
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\Files\\UploadImages\\" + entity.Image);
}
}
catch { }
}
return result;
}
else
return false;
}
catch
{
return false;
}
}


public MVCEshop.Models.DomainModels.Product Find(int id)
{
try
{
return db.Products.Find(id);
}
catch
{
return null;
}
}


public IQueryable<MVCEshop.Models.DomainModels.Product> Where(System.Linq.Expressions.Expression<Func<MVCEshop.Models.DomainModels.Product, bool>> predicate)
{
try
{
return db.Products.Where(predicate);
}
catch
{
return null;
}
}


public IQueryable<MVCEshop.Models.DomainModels.Product> Select()
{
try
{
return db.Products.AsQueryable();
}
catch
{
return null;
}
}


public IQueryable<TResult> Select<TResult>(System.Linq.Expressions.Expression<Func<MVCEshop.Models.DomainModels.Product, TResult>> selector)
{
try
{
return db.Products.Select(selector);
}
catch
{
return null;
}
}


public int GetLastIdentity()
{
try
{
if (db.Products.Any())
return db.Products.OrderByDescending(p => p.Id).First().Id;
else
return 0;
}
catch
{
return -1;
}
}


public int Save()
{
try
{
return db.SaveChanges();
}
catch
{
return -1;
}
}


public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}


protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this.db != null)
{
this.db.Dispose();
this.db = null;
}
}
}


~ProductRepository()
{
Dispose(false);
}
}
}



ممنون میشم

OmMiD_MtWo
دوشنبه 22 آذر 1395, 08:32 صبح
http://stackoverflow.com/questions/18727432/sharprepository-join-between-two-repositories
http://stackoverflow.com/questions/20623022/how-to-join-multiple-tables-using-repository-pattern-entity-framework
http://stackoverflow.com/questions/21758279/how-do-i-join-2-dbsets-in-a-repository-relationship-request-using-linq-entity