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?>'

5
  • possible duplicate of Linq to Entities - Sql "IN" clause
    – Aron
    Feb 12, 2015 at 6:52
  • Try changing the array allId into a List instead Feb 12, 2015 at 6: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, 2015 at 6: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 Feb 12, 2015 at 6: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, 2015 at 4:55

1 Answer 1

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)

2
  • 1
    what's the data type of cc.CATEGORYID? Maybe try casting it to an int? where allId.Contains((int)cc.CATEGORYID)
    – JanR
    Feb 12, 2015 at 6:06
  • What is the type of cc.CATEGORYID? Feb 13, 2015 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.