只有赋值,调用,递增,递减,等待和新对象表达式才能用作语句

时间:2012-08-17 21:26:53

标签: c# vb.net urlencode

我已经将4,000行代码从VB.net转换为c#...我完全被这个问题绊倒了。这不断给我一个错误,“只有赋值,调用增量,减量,等待和新对象表达式才能用作语句”

有关错误的帮助吗?它还抛出一个错误,查询体必须以select子句或group子句结束。

protected static string AssembleArgumentString(Dictionary<string, string> @params,   bool urlEncode)
{
   if (urlEncode) {
     return string.Join("&", (from kv in @params where !string.IsNullOrEmpty(kv.Value)kv.Key + "=" + System.Web.HttpUtility.UrlEncode(kv.Value)).ToArray);
   } 
   else {
     return string.Join("&", (from kv in @params where !string.IsNullOrEmpty(kv.Value)kv.Key + "=" + kv.Value).ToArray);
   } 
 }

2 个答案:

答案 0 :(得分:1)

protected static string AssembleArgumentString(Dictionary<string, string> @params,   bool urlEncode)
{
    if (urlEncode)
    {
        return string.Join("&", (from kv in @params where !string.IsNullOrEmpty(kv.Value)
                                 select kv.Key + "=" + System.Web.HttpUtility.UrlEncode(kv.Value)));

    }
    else
    {
        return string.Join("&", (from kv in @params where !string.IsNullOrEmpty(kv.Value)
                                 select kv.Key + "=" + kv.Value));
    } 
}

答案 1 :(得分:1)

您的LINQ查询中需要select子句。例如:

return string.Join("&", (from kv in @params where !string.IsNullOrEmpty(kv.Value) select kv.Key + "=" + kv.Value).ToArray());

另外,请不要忘记()方法调用结束时的ToArray。您实际上甚至不需要ToArray()因为String.Join()也可以使用IEnumerable