我即将对此感到害怕,我正在使用存储库模式和unityOfwork,如果我使用db.context带来一个列表工作正常如果我在存储库上使用相同的查询带来重复的值,这里是我的代码。
此代码带来正确的数据
var myList = (from p in db.Products_Sizes
where p.productsID == 181 && p.ColorID == 200 && p.SizeID == 133
select new SelectListItem
{
Value = p.eyepower.ToString(),
Text = p.eyepower.ToString()
}).OrderBy(p => p.Value).ToList();
正确的数据是
现在如果我使用此代码。
var myList = _unitOfWork.ProductsSizes.Find(a => a.productsID == 181 && a.ColorID == 200 && a.SizeID == 133).ToList()
.Select(u => new SelectListItem
{
Value = u.eyepower.ToString(),
Text = u.eyepower.ToString()
});
结果
或任何其他重复的
这里是Find`IEnumerable Find(Expression>谓词);在界面
我知道数据是正确的bc我在sql server中运行了一个查询并且数据很好
我无法弄清楚为什么,任何想法?` 这是我的整个结构 基础界面
public interface IRepositoryBase<T> where T : class
{IEnumerable<T> Find(Expression<Func<T, bool>> predicate);}
基础资料库
public class RepositoryBase<T> : IRepositoryBase<T> where T : class
{protected readonly DbContext Context;
public RepositoryBase(DbContext context)
{
Context = context;
}
public T Get(int id)
{
return Context.Set<T>().Find(id);
}
public IEnumerable<T> GetAll()
{
return Context.Set<T>().ToList();
}
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
return Context.Set<T>().Where(predicate);
}}
productsize存储库就像这样
public interface IProductsSizesRepository : IRepositoryBase<Products_Sizes>
{
}
unityOfWork的接口
public interface IUnitOfWork : IDisposable
{
IOrdersRepository ProductSizes{ get; }}
unityOfwork class
public class UnitOfWork : IUnitOfWork
{
public readonly EFDbContext _dbContext;
public UnitOfWork(EFDbContext dbContext)
{
_dbContext = dbContext;
ProductsSizes = new ProductsSizesRepository(_dbContext);
}
public IProductsSizesRepository ProductsSizes { get; private set; }}
最后是我遇到此问题的控制器,你可以看到我正在使用ninject在控制器中注入IUnityOfWork以访问存储库
public class SelectProductController : Controller{private readonly IUnitOfWork _unitOfWork;
public SelectProductController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}}
答案 0 :(得分:0)
我最终将其放入存储库
(from p in db.Products_Sizes
where p.productsID == 181 && p.ColorID == 200 && p.SizeID == 133
select new SelectListItem
{
Value = p.eyepower.ToString(),
Text = p.eyepower.ToString()
}).OrderBy(p => p.Value).ToList();