这可能是一个简单/愚蠢的问题,但是它们是否是一种基于接收到的数据向SQL查询(使用实体框架)添加内部或外部联接的方法?
示例
public bool method(int? typeId, int? categoryId){
var query = from o in _dbContext.SomeObjects;
//JOIN type
if(typeId != null){
//Add inner join with table types to query
//Something like:
//query += join type in _dbContext.Types on o.TypeId equals type.ID
}else{
//Add outer join with table types to query
//query += join type in _dbContext.Types on o.TypeId equals type.ID into types
// from type in types.DefaultIfEmpty()
}
//Do same for category
...
//Filters
if(typeId != null){
query += where type.ID == typeId
}
if(categoryId != null){
query += where category.ID == categoryId
}
}
答案 0 :(得分:1)
我认为您在这里的主要问题只是打字。使用var
存储初始查询会将其键入为DbSet<SomeObject>
。要构建查询,您需要IQueryable<SomeObject>
。换句话说,将您的初始行更改为:
IQueryable<SomeObject> query = from o in _dbContext.SomeObjects;
我自己不使用LINQ-to-SQL,所以可能有些不合时宜,但是我认为您可以这样做:
query = query join type in _dbContext.Types on o.TypeId equals type.ID;
我知道它可以与LINQ-to-Entities一起使用,
query = query.Include(x => x.Types);
答案 1 :(得分:0)
通过执行以下操作修复该问题:
var query = from o in _dbContext.SomeObjects
join type in _dbContext.Types on o.TypeId equals type.ID
where (typeId == null || type.ID == typeId) &&