1

是否可以在 LINQ 查询中检查空值,并且当该值为空时,它是否可以一次执行额外的(子)查询?

解释

我在我的数据库中声明了默认按钮,并带有默认说明。用户可以自定义这些按钮,并且这些设置存储在ButtonLocations表格中。现在,每个按钮都有一个标准的描述,用户可以编辑这个描述。当用户编辑描述时,它存储在Descriptions我数据库的表中。当我检索所有按钮时,我首先检查按钮是否具有特定描述(在buttonlocations中,带有左连接)。如果这不是真的(所以为空),我会检索默认描述。

目前我得到了我所有的实体及其描述,然后我遍历所有实体以检查值是否为空。这会导致对数据库的多次查询。

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);
            }
        }
4

2 回答 2

0

合并运算符在这里应该为您工作。像这样的东西:

.....
select new
{
   Button = button,
   ButtonLocation = bl,
   Description ?? context.Descriptions
                         .FirstOrDefault(
                         x => x.ValueID == button.ButtonID 
                         && x.LanguageID == languageID 
                         && x.DescriptionOriginID == (short)DescriptionOriginEnum.Button)
})
于 2013-10-01T08:35:36.720 回答
0

我认为 Colin 使用 coalesce 运算符的答案应该可以工作,但如果无法做到,你可以尝试做一个子选择,它可以同时获得两个选项,然后按自定义源的偏好排序,并获得最高记录。(我在这里假设任何给定的按钮实际上只有一个描述,并且多个描述不应该导致多个按钮。)

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()
        let description = (
                from d in context.Descriptions
                where
                    d.LanguageID == languageID
                 && (
                      (
                        d.ValueID == bl.ButtonLocationID
                        && d.DescriptionOriginID == descriptionOriginID
                      )
                    ||
                      (
                        d.ValueID == b.ButtonID 
                        d.DescriptionOriginID == (short)DescriptionOriginEnum.Button
                      )
                    )
                // this line puts custom descriptions first
                orderby d.DescriptionOriginID == (short)DescriptionOriginEnum.Button
                        ? 1
                        : 0
                select d
                )
                // this will get a custom description if there was one, otherwise
                // the first one will be the default description
                .FirstOrDefault() 
        where bl.ButtonGroupID == buttonGroupId
        select new
        {
          Button = button,
          ButtonLocation = bl,
          Description = description
        })
        .ToList();

这显然有点尴尬,而且可能不是最有效的查询。我会尝试先将合并运算符移动到一条let description = d ?? /*subselect*/线上。

于 2013-10-01T11:09:33.300 回答