0

I am trying to mimic an IN() clause commonly used in SQL and use it in my LINQ statement. I see that there is an overload for Contains() that takes an IEnumerable collection. I have tried passing in ILIST and Dictionary but neither is correct. How do I accomplish this?

Thanks, much appreciated.

SQL

select 
oec.OnlineEducationCourseId, 
oec.CourseTitle,COUNT(oec.CourseTitle) as CourseCount
from OnlineEducationRegistration as oer
join OnlineEducationCourse oec on oec.OnlineEducationCourseId = oer.OnlineEducationCourseId
where oer.ClubId IN('K20','B67','P89')
and DateCompleted between '2013-01-01' and '2014-01-01'
group by oec.CourseTitle,oec.OnlineEducationCourseId

Same SQL query in LINQ

var r = (from oer in db.OnlineEducationRegistrations
join oec in db.OnlineEducationCourses on oer.OnlineEducationCourseId equals
oec.OnlineEducationCourseId
where (oer.ClubId.Contains(some IEnumerable collection here) && oer.DateCompleted >= start.Date && oer.DateCompleted <= end.Date)
group new { oec, oer } by new { oec.CourseTitle, oec.OnlineEducationCourseId }).ToList();
Slinky
  • 5,662
  • 14
  • 76
  • 130
  • This `where (oer.ClubId.Contains(some IEnumerable collection here)` should be other way round like `where (SomeIEnumerable.Contains(oer.ClubId))` – Habib Sep 26 '14 at 19:10
  • What do you mean, _"but neither is correct"_? Doesn't it work (if not, please be more specific and post a code example that reproduces the issue, not just _"some IEnumerable collection here"_), or do you simply dislike your solution somehow? – stakx - no longer contributing Sep 26 '14 at 19:11
  • @stakx Sorry for leaving out some important details. The error was caused by the value I was giving to Contains(). I was giving it a collection when it really needed a single value – Slinky Sep 29 '14 at 11:48

0 Answers0