LINQ クエリで null 値をチェックし、値が null の場合、余分な (サブ) クエリを一度に実行することはできますか?
説明
データベースで宣言されたデフォルトのボタンがあり、デフォルトの説明があります。ユーザーはこれらのボタンをカスタマイズでき、これらの設定はButtonLocations
テーブルに保存されます。現在、すべてのボタンには標準の説明があり、ユーザーはこの説明を編集できます。ユーザーが説明を編集するとDescriptions
、データベースのテーブルに保存されます。すべてのボタンを取得するときは、最初にボタンに特定の説明があるかどうかを確認します ( buttonlocationsで左結合を使用)。これが当てはまらない (null の場合) 場合は、デフォルトの説明を取得します。
現在、すべてのエンティティとその説明を取得し、その後、それらすべてをループして値が null かどうかを確認します。これにより、データベースへの複数のクエリが発生します。
var temp = (from bl in context.buttonLocations
join b in context.Buttons
on bl.ButtonID equals b.ButtonID into buttons
from button in buttons.DefaultIfEmpty()
join d in context.Descriptions
on new
{
ID = bl.ButtonLocationID,
langID = languageID,
originID = descriptionOriginID
}
equals new
{
ID = d.ValueID,
langID = d.LanguageID,
originID = d.DescriptionOriginID
}
into other
where bl.ButtonGroupID == buttonGroupId
from x in other.DefaultIfEmpty()
select new
{
Button = button,
ButtonLocation = bl,
Description = x
}).ToList();
// Retrieve default descriptions if no specific one is set
foreach (var item in temp)
{
if (item.Description == null)
{
item.Description = context.Descriptions
.FirstOrDefault(x => x.ValueID == item.Button.ButtonID && x.LanguageID == languageID && x.DescriptionOriginID == (short)DescriptionOriginEnum.Button);
}
}