我有两个表,操作代码和子操作代码,我需要返回每个代码的所有子代码,只有活动的子代码
public class OperationCode: Entity
{
public guid Id.
public bool Active{ get; set; }
public bool Excluded{ get; set; }
public string Code { get; set; }
public virtual ICollection<SubOperationCode> SubOperationCode{ get; set; }
}
public class SubOperationCode: Entity
{
public guid Id.
public bool Active{ get; set; }
public bool Excluded{ get; set; }
public string Code { get; set; }
public virtual OperationCode OperationCode{ get; set; }
}
我尝试使用类似的方法,但是没有用
public override OperationCode getById(Guid id)
{
var result = Db.SubOperationCode.Join(Db.OperationCode,
sub => sub.Id == id,
code => code.Id,
(sub, code) => new { sub, code }).toList();
return result
}
答案 0 :(得分:0)
您不需要JOIN-使用导航属性,然后投影到结果列表(在这种情况下为匿名,但可以是模型)。
var operationsWithActiveSubOps = Db.OperationCodes
.Include(o => o.SubOperationCode)
.Select(o => new {
OperationId = o.Id,
OperationCode = o.Code,
ActiveSubOperations = o.SubOperationCode.Where(so => so.Active)
})
.ToList();
答案 1 :(得分:0)
您可以使用以下
public override OperationCode getById(Guid id)
{
var result = Db.SubOperationCode.Join(Db.OperationCode,
sub => sub.Id,
code => code.Id,
(sub, code) => new { sub, code })
Where(x => sub.Id == id).toList();
return result
}
答案 2 :(得分:0)
您可以尝试以下一种方法:
public override OperationCode getById(Guid id)
{
return Db.OperationCode.Include(o => o.SubOperationCode)
.FirstOrDefault(o => o.Id == id && o.SubOperationCode.Any(so => so.Active))
}