我有一组使用外键链接的表
A 1----* B
也就是说,A可以有很多B记录。
我需要添加一个“where”子句来返回所有A记录,其中B.isMax == true如果某个标志传入我的函数。我不知道如何处理这个问题。
public List<A> GetA(int AID, string AName, bool? isActive, bool? isMax)
{
var q = from a in Context.A
select a;
if (AID > 0)
{
q = q.Where(c => c.AID == AID);
}
if (!string.IsNullOrEmpty(AName))
{
q = q.Where(c => c.Name.Contains(AName));
}
if (isActive != null)
{
q = q.Where(c => c.IsActive == isActive);
}
if (isMax != null)
{
// ???? Can't do this. How can I implement this kind of thing??
q = q.Where(c => c.B.IsMax == isMax);
}
List<A> ret = q.ToList();
return ret;
}
有什么想法吗?
答案 0 :(得分:2)
答案 1 :(得分:1)
您的意思是Any
吗?
q = q.Where(c => c.B.Any(b => b.IsMax == isMax));
答案 2 :(得分:0)
你的想法,这个好主意是做这样的事吗?
public List<A> GetA(int AID, string AName, bool? isActive, bool? isMax)
{
return Context.A
.Where(c =>
(AID <= 0 || c.AID == AID)
&& (string.IsNullOrEmpty(AName) || c.Name.Contains(AName))
&& (isActive == null || c.IsActive == isActive)
&& (isMax == null || c.B.Any(b => IsMax == isMax))
)
.ToList();
}