我想使用EF加入2个表并检查是否有值
public bool IsSubscriptionExist(string domain)
{
try
{
using (AccContext db = new AccContext ())
{
var count = (from s in db.Subscriptions
join a in db.Allias on s.Id equals a.Subscription_Id
where (s.Domain == domain || a.Allias_Domain == domain)
select s).Count();
return count > 0;
}
}
catch (Exception ex)
{
customLogManager.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
throw ex;
}
}
问题是当订阅记录存在时count返回0,我认为因为Allias不存在。这与我认为的加入/左连接相同。
即使Allias不存在,有没有办法计算?
答案 0 :(得分:1)
如上所述:
https://code.msdn.microsoft.com/LINQ-Join-Operators-dabef4e9#leftouterjoin
public bool IsSubscriptionExist(string domain)
{
try
{
using (AccContext db = new AccContext ())
{
var count = (from s in db.Subscriptions
join a in db.Allias on s.Id equals
a.Subscription_Id into ps
from a in ps.DefaultIfEmpty()
where (s.Domain == domain || a.Allias_Domain == domain)
select s).Count();
return count > 0;
}
}
catch (Exception ex)
{
customLogManager.Error(System.Reflection.MethodBase.GetCurrentMethod().Name, ex);
throw ex;
}
}
希望它对您有用