C# 类型为'的未处理异常;系统。无效卡斯特例外';发生…无法强制转换类型为';System.String';打字

C# 类型为'的未处理异常;系统。无效卡斯特例外';发生…无法强制转换类型为';System.String';打字,c#,linq,C#,Linq,谁能帮我一下吗?遵循我的代码,并且在GelatoProject.exe中发生“System.InvalidCastException”类型的未处理异常 其他信息:无法将类型为“System.String”的对象强制转换为类型为“GelatoProject.RecipeVariables”的对象。消息为 // ... using (var db = new GelatoProjectDBEntities()) { RecipeVariables selected = (R

谁能帮我一下吗?遵循我的代码,并且在GelatoProject.exe中发生“System.InvalidCastException”类型的未处理异常

其他信息:无法将类型为“System.String”的对象强制转换为类型为“GelatoProject.RecipeVariables”的对象。消息为

// ...
using (var db = new GelatoProjectDBEntities())
{
    RecipeVariables selected = 
        (RecipeVariables)comboBoxRecipeDetail.SelectedItem;

    var results = (from x in db.Recipe_Parameters
                select new RecipeVariables 
                        { 
                            Id = x.Id, 
                            RecipeDetail = x.recipeDetail, 
                            Parameter = x.parameter, 
                            RangeDetail = x.rangeDetail, 
                            RangeValue = x.value.ToString() 
                        }
               )
                .Where(x => x.RecipeDetail == selected.RecipeDetail && x.Parameter == "totalsolids" && x.RangeDetail == "Max")
                .FirstOrDefault();

    totsolidsRangeMax = decimal.Parse(results.RangeValue);

    MessageBox.Show(totsolidsRangeMax.ToString());
}
// ...

class RecipeVariables
{
    public int Id { get; set; }
    public string NameIngredient { get; set; }
    public string Brand { get; set; }
    public string LBName { get { return NameIngredient + " - " + Brand;}}
    public string RecipeDetail { get; set; }
    public string Parameter { get; set; }
    public string RangeDetail { get; set; }
    public string RangeValue { get; set; }
}
ComboboxRecipedDetail.SelectedItem
是一个字符串-单击组合框时可以看到的文本。它不能强制转换为
可接受变量

将代码更改为:

using (var db = new GelatoProjectDBEntities())
{
    RecipeVariables selected = new RecipeVariables()
                               {
                                   RecipeDetail = (string)comboBoxRecipeDetail.SelectedItem
                               };

    // var results = ...
}
这将创建一个新的RecipeVariables对象,然后将其RecipeDetail属性设置为所选组合框项的文本

ComboboxRecipedDetail.SelectedItem
是一个字符串-单击组合框时可以看到的文本。它不能强制转换为
可接受变量

将代码更改为:

using (var db = new GelatoProjectDBEntities())
{
    RecipeVariables selected = new RecipeVariables()
                               {
                                   RecipeDetail = (string)comboBoxRecipeDetail.SelectedItem
                               };

    // var results = ...
}

这将创建一个新的RecipeVariables对象,然后将其RecipeDetail属性设置为所选组合框项目的文本。

谢谢Tadija的回复!我将用RecipeVariables代码编辑主代码。我编辑了代码。另外,您的下一个问题可能是
RangeValue=x.value.ToString()
,无法将类型“object”隐式转换为“string”。存在显式转换(是否缺少转换?)现在我遇到了x.value.ToString()问题。。。其他信息:LINQ to Entities无法识别方法“System.String ToString()”方法,并且无法将此方法转换为存储表达式。这是一个不相关的问题,请打开另一个问题,以便我也可以回答。顺便说一句,这解决了你目前的问题吗?谢谢Tadija的回复!我将用RecipeVariables代码编辑主代码。我编辑了代码。另外,您的下一个问题可能是
RangeValue=x.value.ToString()
,无法将类型“object”隐式转换为“string”。存在显式转换(是否缺少转换?)现在我遇到了x.value.ToString()问题。。。其他信息:LINQ to Entities无法识别方法“System.String ToString()”方法,并且无法将此方法转换为存储表达式。这是一个不相关的问题,请打开另一个问题,以便我也可以回答。顺便问一下,这解决了你现在的问题吗?