【问题标题】:Sequence operators not supported for type 'System.String'.“System.String”类型不支持序列运算符。
【发布时间】:2016-11-02 01:57:06
【问题描述】:

以下代码的 where 条件中的列上有一个复合主键,因此只能有一个可能的返回值。我通过SingleOrDefault 返回,但在运行时我收到问题标题中的错误:“'System.String' 类型不支持序列运算符。”我对 LINQ 很陌生,所以可能语法错误?任何帮助表示赞赏

 using (myContext Data = new myContext())
    {
        string Result = (from d in Data.myTable 
                    where d.textColumn == myDropDownList.SelectedValue && d.intColumn == 1
                    select d.anotherTextColumn.SingleOrDefault()).ToString();
    }

【问题讨论】:

  • 我认为你在错误的事情上调用 SingleOrDefault,在括号内而不是在外面。这个怎么样:选择 d.anotherTextColumn**)**.SingleOrDefault()
  • dotnetom 的回答是正确的,但如果您想知道,您不能在 linq-to-entities 查询中调用 ToString,因为该查询必须被转换为 sql,而不是像往常一样执行.

标签: c# asp.net linq


【解决方案1】:

您快到了 - 您需要在查询对象上调用 SingleOrDefault() 而不是在 string 对象上调用它:

string Result = (from d in Data.myTable 
                where d.textColumn == myDropDownList.SelectedValue && d.intColumn == 1
                select d.anotherTextColumn).SingleOrDefault();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多