0

I have this part of code:

ViewBag.myFeedbacks = db.feedbacks.Where(f => f.project_id ?)

what should I replace char ? , when I want define something like this :

project_id IN (1,2,3)

so, select all feedback, which have project_id from list of values?

Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
user5503823
  • 19
  • 1
  • 5

3 Answers3

3
ViewBag.myFeedbacks = db.feedbacks.Where(f => new[]{1,2,3}.Contains(f.project_id));

IQueryable.Contains is specialized to emit ... in ... SQL code.

Blindy
  • 65,249
  • 10
  • 91
  • 131
2
var projectIds = new[]{1, 2, 3};

ViewBag.myFeedbacks = db.feedbacks.Where(f => projectIds.Any(p => p == f.project_id))
vidalsasoon
  • 4,365
  • 1
  • 32
  • 40
1

Something like this:

db.feedbacks.Where(f => yourArray.Contains(f.project_id))
xZ6a33YaYEfmv
  • 1,816
  • 4
  • 24
  • 43
  • http://stackoverflow.com/questions/7897630/why-does-the-contains-operator-degrade-entity-frameworks-performance-so-drama – vidalsasoon Nov 03 '15 at 18:55
  • please specify your question or comment to my answer. if it's connected to bad performance of `Contains` expression I must assure you that since EF6 this was successfully fixed and works relatively fast now. – xZ6a33YaYEfmv Nov 03 '15 at 18:58
  • just in case he wasn't using EF6+ which is quite possible. – vidalsasoon Nov 03 '15 at 20:05