简体   繁体   中英

Sequence operators not supported for type 'System.String'.

There is a composite primary key on the columns in the where condition of the below code so there can be only one possible return value. I am returning by SingleOrDefault but at run time I am getting the error in the question title: "Sequence operators not supported for type 'System.String'." I'm pretty new to LINQ so might have got the syntax wrong? Any help appreciated

 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();
    }

You're almost there - you need to call SingleOrDefault() on the query object instead of calling it on string object:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM