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.

Related posts

Comments are closed

Cliff Gray's Info

Cliff Gray
Developer/Founder GrayTechnology.com.

E-mail me Send mail

Authors

Calendar

<<  September 2019  >>
MoTuWeThFrSaSu
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

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 2019

Subscribe