1

I want to use sql in cluase in linq for that i have tried contains but its showing error. My Code is..

 var allId = new int[] { 31, 32, 33 };
 var libraryList = from c in db.COURSELIBRARies
                   join cc in db.COURSECATEGORYTAGGINGs on c.LIBITEMID equals cc.COURSEID
                   where allId.Contains(cc.CATEGORYID) // here error is showing.
                   select new Library { Id = c.LIBITEMID, Name = c.GROUPNAME, Desc = c.DESCRIPTION, logo = c.LOGO, CategoryId = cc.CATEGORYID };

Error is :

'int[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Queryable.Contains<TSource>(System.Linq.IQueryable<TSource>, TSource)' has some invalid arguments

Instance argument: cannot convert from 'int[]' to 'System.Linq.IQueryable<int?>'

Community
  • 1
  • 1
Raghubar
  • 2,768
  • 1
  • 21
  • 31
  • possible duplicate of [Linq to Entities - Sql "IN" clause](http://stackoverflow.com/questions/857973/linq-to-entities-sql-in-clause) – Aron Feb 12 '15 at 06:52
  • Try changing the array `allId` into a List instead – Jens Kloster Feb 12 '15 at 06:53
  • That particular error is due to one of two things. Either you are lying and are not actually using `Entity Framework` but are instead using `Linq to SQL` or you are using `Entity Framework 2`, which does not support `IEnumerable.Contains`. – Aron Feb 12 '15 at 06:55
  • your first 3 lines of code doenst work. Splitting `{31,32,33}` with `.Split(',')` and the parsing the elements as `int` fails because `"{31" != int` – Jens Kloster Feb 12 '15 at 06:58
  • @JensKloster by mistake i have written that actually its value coming dynamical and that is 31,32,33, so its working. – Raghubar Feb 13 '15 at 04:55

1 Answers1

1

Have you included;

using System.Linq;

?

Another possible cause would be that the value you are passing into Contains() is not an INT. Try casting it: where allId.Contains((int)cc.CATEGORYID)

JanR
  • 6,052
  • 3
  • 23
  • 30