LINQ Sequence contains more than one element

by Cliff 24. July 2009

Recently I ran into an issue with the contains method in LINQ in conjunction with the SingleOrDefault method.

My query was similar to:

var data = (from i in ctx.DataContext.orders
where order.Contains(i.Name)
select i).SingleOrDefault();

and this was throwing the error:

System.InvalidOperationException: Sequence contains more than one element

Turns out the Contains translated to a LIKE '%...%' and was returning multiple records.  By changing this to i.Name == order, then I got back the single result I was expecting.

Comments are closed

Cliff Gray's Info

Cliff Gray
Developer/Founder GrayTechnology.com.

E-mail me Send mail

Authors

Calendar

<<  May 2022  >>
MoTuWeThFrSaSu
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar

Blogroll

Download BlogEngine.NET

Download at CodePlex

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2022

Subscribe