我有以下课程
public class Response
{
public Prompts Prompts { get; set; }
}
public class Prompts
{
public List<Prompt> prompt { get; set; }
}
public class Prompt
{
public object message { get; set; }
public object details { get; set; }
}
我正在尝试检索其prompt
包含&#39; Hello&#39;的message
邮件。但它给了我错误:
无法隐式地从提示转换为字符串
到目前为止,我尝试了这个lambda表达式:
string message = Response.Prompts.prompt.Find(p => p.message.ToString().Contains("Hello"));
答案 0 :(得分:7)
此行应返回object
ToString()
如果您只需要访问该属性所需的消息。由于它是string message = pr.message.ToString();
类型,因此您需要调用string
方法
string message = (string)pr.message;
或将其投放到Prompt pr = Response.Prompts.prompt.Find(
p => p.message.ToString().ToLower().Contains("Hello".ToLower()));
null
如果您需要它不区分大小写:
string message = pr != null pr.message.ToString() : "";
由于Find(...)
在找不到匹配项时会返回class Food
abstract class Animal {
type SuitableFood <: Food
def eat(food: SuitableFood)
}
class Grass extends Food
class Cow(name: String) extends Animal {
type SuitableFood = Grass
override def eat(food: Grass) { println(s"cow '${name}' eats ${food}") }
}
class Fish extends Food
val bessy: Animal = new Cow("bessy")
// Fails to compile, as expected
bessy eat (new Fish)
,因此建议您在访问属性时检查它:
Grass