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

我有这个foreach部分,我试图在我的“result = string.Format”之后添加一行但是我得到以下错误“只有赋值,调用,递增,递减,等待和新对象表达式可以用作语句“有人能告诉我我做错了什么。

foreach (DataRow record in table.Rows) { string key = record["Code"] + "_" + record["Description"]; int row = (int)rownum[key]; string date = formatDate(record["ApptDate"].ToString(), "_"); string result = string.Empty; if (record["Dosage"].ToString() != string.Empty) result = string.Format("{0}/{1}", test.SharedLib.Application.RemoveTrailingZeroes(record["Dosage"].ToString()), test.SharedLib.Application.RemoveTrailingZeroes(record["Units"].ToString())); if (record["Dosage"].ToString() != string.Empty) result.StartsWith("/") && result.EndsWith("/") ? result.Replace("/", string.Empty) : result; else if (record["Units"].ToString() != string.Empty) result = record["Units"].ToString(); dataTable.Rows[row]["ApptDate" + date] = result; } 

 if (record["Dosage"].ToString() != string.Empty) result.StartsWith("/") && result.EndsWith("/") ? result.Replace("/", string.Empty) : result; 

第二行没有声明,它是一个表达式。 并非所有表达式都可以是C#中的语句,因此这是语法错误。

大概你打算将结果分配给result

 if (record["Dosage"].ToString() != string.Empty) result = (result.StartsWith("/") && result.EndsWith("/")) ? result.Replace("/", string.Empty) : result; 

此外,您应该考虑用大括号( {} )括起if / else块的主体。 没有支撑,这些块嵌套的方式不直观,并且会妨碍将来的维护。 (例如,你能告诉哪个块else if块是否属于哪个?inheritance这个项目的下一个人是否能够不仅能说出差异,还能理解嵌套是什么意思 ?明确它!)

 result.StartsWith("/") && result.EndsWith("/") ? result.Replace("/", string.Empty) : result; 

该行对三元运算符的结果不起作用。 我假设你想把它分配给结果

你必须对表达式的结果做一些事情(例如将它分配给结果?!)

有关更多信息,您应该格式化并告诉我们有关您的代码的更多信息… SO用户不是您的个人编译器/调试器;-)