在 LINQ 中使用 Lambda 表达式级联下拉列表
我有一系列级联的下拉列表,但不是根据用户的选择从列表中排除列表选项,而是禁用列表中的不相关选项。它们仍然存在于下拉列表中,但呈灰色,用户无法选择它们,并且它们不会包含在提交中。
我通过列表选项和适用于用户当前选择的列表选项子集之间的左连接来实现此目的。如果子集中缺少主列表的成员,则将返回 null,我可以提供布尔值来指示是否禁用该选项。
假设我有三个级联下拉列表,与以下三个表相关联:OptionA、OptionB 和 OptionC。 vwOptionIndex是一个视图,它对当前正在使用的OptionA、OptionB和OptionC的所有组合进行索引,从而为级联提供适用的子集。
如果用户在选项 B 中选择两个值 (123, 456),在选项 C 中选择一个值 (789),那么要返回选项 A 的列表,我将使用以下 SQL Server 查询。
SELECT
a.ID,
a.Label,
CAST(
CASE
WHEN SUM(
CASE
WHEN x.OptionA_ID IS NULL
THEN 0
ELSE 1
END
) > 0
THEN 0
ELSE 1
END AS BIT
) AS [Disabled]
FROM dbo.OptionA AS a
LEFT OUTER JOIN dbo.vwOptionIndex AS x
ON a.ID = x.OptionA_ID
AND OptionB_ID IN (123, 456)
AND OptionC_ID IN (789)
GROUP BY a.ID, a.Label
我试图在 LINQ 中表示这个查询。我已经弄清楚了连接,但我无法弄清楚如何表示返回指示是否应禁用该列表选项的位的查询部分。
from t1 in OptionAs
join t2 in VwOptionIndex
on new { t1.ID} equals new { t2.OptionA_ID } into j1
from j2 in j1.DefaultIfEmpty()
group j2 by new { t1.ID, t1.Label } into g
select new { g.Key.ID, g.Key.Label, Disabled = <???> }
换句话说,我需要在上面的 LINQ 查询表达式中使用以下逻辑。
CAST( CASE WHEN SUM( CASE WHEN x.OptionA_ID IS NULL THEN 0 ELSE 1 END ) > 0 THEN 0 ELSE 1 END AS BIT ) AS [Disabled]
任何帮助将不胜感激。
谢谢!
I have a series of drop down lists that cascade, but instead of the list options being excluded from the list according to the user's selection, the unrelated options in the list are disabled. They are still present in the drop down list, but they are greyed out, the user cannot select them and they will not be included in the submit.
I accomplish this with a left join between the list options and a subset of the list options that are applicable to the user's current selection. If a member of the main list is missing from the subset, that will return null and I can case that to provide the boolean that indicates whether not that option will be disabled.
Suppose I have three cascaded drop down lists tied to the following three tables: OptionA, OptionB and OptionC. vwOptionIndex is a view that indexes all the combinations of OptionA, OptionB and OptionC that are currently in use, thus providing the applicable subset for the cascading.
If the user selects two values in OptionB (123, 456) and one value in OptionC (789), then to return the list for OptionA I would use the following SQL Server query.
SELECT
a.ID,
a.Label,
CAST(
CASE
WHEN SUM(
CASE
WHEN x.OptionA_ID IS NULL
THEN 0
ELSE 1
END
) > 0
THEN 0
ELSE 1
END AS BIT
) AS [Disabled]
FROM dbo.OptionA AS a
LEFT OUTER JOIN dbo.vwOptionIndex AS x
ON a.ID = x.OptionA_ID
AND OptionB_ID IN (123, 456)
AND OptionC_ID IN (789)
GROUP BY a.ID, a.Label
I am trying to represent this query in LINQ. I've figured out the join, but i haven't been able to figure out how to represent the portion of the query that returns the bit that tells whether or not that list option should be disabled.
from t1 in OptionAs
join t2 in VwOptionIndex
on new { t1.ID} equals new { t2.OptionA_ID } into j1
from j2 in j1.DefaultIfEmpty()
group j2 by new { t1.ID, t1.Label } into g
select new { g.Key.ID, g.Key.Label, Disabled = <???> }
In other words, I need the following bit of logic in the LINQ query expression above.
CAST( CASE WHEN SUM( CASE WHEN x.OptionA_ID IS NULL THEN 0 ELSE 1 END ) > 0 THEN 0 ELSE 1 END AS BIT ) AS [Disabled]
Any help would be greatly appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上不需要
Sum
,您只需要检查组中是否有不为空的值。You don't actually need the
Sum
, you only need to check whether there are any values in the group that are not null.我相信我已经弄清楚了。
希望这会帮助其他人。
I believe I figured it out.
Hopefully this will help someone else out.