首页 \ 问答 \ 如何优化嵌套条件SQL Server查询2008R2(How to optimize nested conditional SQL Server Query 2008R2)

如何优化嵌套条件SQL Server查询2008R2(How to optimize nested conditional SQL Server Query 2008R2)

if {condition1} = '0'
begin
    if  {condition2} = 'Yes'
        Begin
            Set @SQLQuery = @SQLQuery + ' AND  '
        end
    else 
        begin
            Set @SQLQuery = @SQLQuery + ' AND  '
        end
end

if {condition1} = '0'
begin
    if  {condition2} = 'Yes'
        Begin
            Set @SQLQuery = @SQLQuery + ' AND  '
        end
    else 
        begin
            Set @SQLQuery = @SQLQuery + ' AND  '
        end
end

原文:https://stackoverflow.com/questions/37066834
更新时间:2022-01-20 10:01

最满意答案

如果两个列表都是有序的,您可以使用Zip扩展方法:

tokens.Cast<TokensEnum>().Zip(names, (t, n) => Tuple.Create(t, n));

这将返回IEnumerable<Tuple<TokensEnum, string>>

但正如Jon Skeet所暗示的那样 ,在这种情况下,做这样的事情可能会更容易:

Enum.GetValues(typeof(TokensEnum))
    .Cast<TokensEnum>()
    .Select(t => Tuple.Create(t, t.ToString()));

这将返回与上面相同的结果,但它将在一个步骤中完成。

既然你在评论中提到你想要一个Dictionary<string, TokensEnum> ,你可以使用这样的东西来构造它:

Dictionary<string, TokensEnum> myDictionary = 
    Enum.GetValues(typeof(TokensEnum))
        .Cast<TokensEnum>()
        .ToDictionary(t => t.ToString());

If both lists are in order, you can use the Zip extension method:

tokens.Cast<TokensEnum>().Zip(names, (t, n) => Tuple.Create(t, n));

This will return an IEnumerable<Tuple<TokensEnum, string>>.

But as Jon Skeet suggests, in this case, it would probably be easier to do something like this:

Enum.GetValues(typeof(TokensEnum))
    .Cast<TokensEnum>()
    .Select(t => Tuple.Create(t, t.ToString()));

This will return the same result as above, but it will do it in a single step.

Since you mentioned in your comments that you'd like to have a Dictionary<string, TokensEnum>, you can use something like this to construct it:

Dictionary<string, TokensEnum> myDictionary = 
    Enum.GetValues(typeof(TokensEnum))
        .Cast<TokensEnum>()
        .ToDictionary(t => t.ToString());

相关问答

更多