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

2
  • I have tried that also but i get another error like "int[] does not contain a definition for 'Contains' and the best extension overload..." Commented May 28, 2015 at 12:50
  • Check that you have using System.Linq; at the top of your .cs code file. Commented May 28, 2015 at 12:57

2 Answers 2

10

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)

Sign up to request clarification or add additional context in comments.

6 Comments

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.
OK, I think this 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.
hmmmm, that can be... I will check and answer u again.
Unfortunatelly:( There is no problem in db and model. its integer value. I even recreated the model and still same error.
It worked with an extra '(int)' m => arrayOfInts.Contains((int)m.MainMusleGroupID)
|
3

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();
            }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.