追加の句のみが異なる2つのクエリのいずれかを実行する以下の条件があります
&& acc.Fb_DataSourceKey == dskId
条件はこちら
var statData = dskId != -1 ? from s in dao.fb_statdata
join acc in dao.fb_datasourceadaccount
on s.Fb_DataSourceAdAccountId equals acc.Id
where s.ReportTypeId == 1
&& acc.Fb_DataSourceKey == dskId
group new { s, acc.Fb_DataSourceKey } by new { s.Fb_DataSourceAdAccountId, s.Start_time } into res
select res
:
from s in dao.fb_statdata
join acc in dao.fb_datasourceadaccount
on s.Fb_DataSourceAdAccountId equals acc.Id
where s.ReportTypeId == 1
group new { s, acc.Fb_DataSourceKey } by new { s.Fb_DataSourceAdAccountId, s.Start_time } into res
select res;
今、 https://stackoverflow.com/a/2850048/1170932のおかげで、以下に変更できることがわかりました
var statData = from s in dao.fb_statdata
join acc in dao.fb_datasourceadaccount
on s.Fb_DataSourceAdAccountId equals acc.Id
where s.ReportTypeId == 1
group new { s, acc.Fb_DataSourceKey } by new { s.Fb_DataSourceAdAccountId, s.Start_time } into res
select res;
if (singleDsk)
statData = from s in statData where s.Key.Fb_DataSourceAdAccountId == dskId select s;
これにより、コードの重複がなくなりますが、ひどく非効率的な外部結合が使用されます (当然のことながら、実際には)。FTR では、DB に MySQL を使用しています。
残念ながら、外部結合は容認できないほど遅いです。外部結合を生成せず、コードを複製せずにクエリ スタイルの条件を実行する方法はありますか?