1
public void GetMuscles(List<ListItem> selected)
 {
     int[] arrayOfStrings = new int[selected.Count];

     for (int i = 0; i < selected.Count; i++)
     {
         arrayOfStrings[i] = Convert.ToInt32(selected[i].Value);
     }  
}  

using (var db = new DWSEntities())
{
     var muscles = (from m in db.Muscles
         where m.MainMusleGroupID. //ISSUE
         select new { m.MusleName, m.ID }).Take(40);
}

In where statement I need to use contains but after "." I don't have option to use contains. I tried the same with a non integer value and it appears. I have an integer array and need WHERE IN clause. So is there any other way to do it without contains or how can I use contains with integer values?

casperOne
  • 73,706
  • 19
  • 184
  • 253
CanESER
  • 301
  • 1
  • 3
  • 19
  • I have tried that also but i get another error like "int[] does not contain a definition for 'Contains' and the best extension overload..." – CanESER May 28 '15 at 12:50
  • Check that you have `using System.Linq;` at the top of your .cs code file. – StuartLC May 28 '15 at 12:57

2 Answers2

9

My comment

Sql WHERE... IN is inverted in Linq i.e.

 where MyIntArray.Contains(m.MainMusleGroupID)

or in lambda syntax

db.Muscles.Where(m => MyIntArray.Contains(m.MainMusleGroupID)

Re : int[] does not contain a definition for 'Contains

At the top of the class, ensure you have:

 using System.Linq;

(and the below comment) Ensure that the type of the Entity field used in the Contains matches the type of the collection being compared to, i.e. MainMusleGroupID must itself be an integer, in order to be used with arrayOfInts.Contains().

Re : Refactoring

The naming convention arrayOfStrings to indicate an array of integer (or actually, other numeric type, e.g. long) will cause maintainability issues.

You can simplify the creation of the arrayOfX[] array with LINQ: (note you have a extra } between the for loop and the using):

var arrayOfLongs = selected.Select(s => Convert.ToInt64(s.Value)).ToArray();

Result

var muscles = (from m in db.Muscles
    where arrayOfLongs.Contains(m.MainMusleGroupID)
    select new { m.MusleName, m.ID }).Take(40);

Or in Lambda syntax:

var musclesLambda = db.Muscles
    .Where(m => arrayOfLongs.Contains(m.MainMusleGroupID))
    .Select(m => new { m.MusleName, m.ID })
    .Take(40);

(I've changed the name of the array to match the type)

StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • Same error "int[] does not contain a definition for 'Contains' and the best extension overload..." :( I think its just for string values. Cos it works if I use string values. – CanESER May 28 '15 at 13:12
  • OK, I think [this](http://stackoverflow.com/q/1069106/314291) might be the issue - `MainMusleGroupID` probably isn't an `int` - possibly you've made some model changes (e.g. to `Enum`) or possibly its a `Guid`, Varchar, or other type. The type of `arrayOfStrings` will need to be adjusted accordingly. – StuartLC May 28 '15 at 13:33
  • hmmmm, that can be... I will check and answer u again. – CanESER May 28 '15 at 13:35
  • Unfortunatelly:( There is no problem in db and model. its integer value. I even recreated the model and still same error. – CanESER May 28 '15 at 13:39
  • It worked with an extra '(int)' m => arrayOfInts.Contains((int)m.MainMusleGroupID) – CanESER May 28 '15 at 13:48
  • Which would I guess mean that `MainMusleGroupID` isn't integer :). I guess it will be something like `long`, in which case the better solution is to make `arrayOfStrings` a `long []` – StuartLC May 28 '15 at 13:51
4

I found the solution. Just I added (int) here:

m => arrayOfInts.Contains((int)m.MainMusleGroupID) 

this is working code below:

var arrayOfInts = selected.Select(s => Convert.ToInt32(s.Value)).ToArray();
            using (var db = new DWSEntities())
            {
                var muscles = db.Muscles.Where(m => arrayOfInts.Contains((int)m.MainMusleGroupID))
                                        .Select(m => new { m.MusleName, m.ID }).Take(40);



                cblMusle.DataSource = muscles.ToList();
                cblMusle.DataTextField = "MusleName";
                cblMainMuscle.DataValueField = "ID";
                cblMusle.DataBind();
            }
CanESER
  • 301
  • 1
  • 3
  • 19