SqlException because Subquery returned more than 1 value

前端 未结 2 983
眼角桃花
眼角桃花 2021-01-28 02:10

I have the following LINQ query that I am using to construct a structure to stuff into a JavaScript grid library which is irrelevant for this example, but I figured I would stil

相关标签:
2条回答
  • 2021-01-28 02:15

    The SingleOrDefault call will throw an exception if the expression returns more than one item. Maybe you could try and use FirstOrDefault if its just the Top 1 you want.

    0 讨论(0)
  • 2021-01-28 02:25

    use the DataContext.Log property to display the sql generated by your query. You most likely have a sub-query that is generating more than one result when only one result is valid. For example the following sql will fail if more than one result is returned in the sub-query:

    Select * from orders where customer_id = 
      (select customer_id from customer where name ='bob')
    

    The equality of the where clause in the main query makes no sense if there is more than one result returned from the sub-query.

    You may need to alter the uniqueness of some columns of data in your storage in order to ensure that only one row is returned in the sub-query. Another alternative is to alter your class so that the specific problem property being assigned to is a collection instead of a single value.

    0 讨论(0)
提交回复
热议问题