0

hi i am trying to find list.contains from my int array basically i am matching list items from my int array

int[] questionName =
{
    19294,
    19300,
    20575,
    20826,
    20827
};

var ans = (from answer in db.tblAnswers
    where answer.tblQuestion.bIsRequired == true
        && questionName.Contains(Convert.ToInt32(answer.nQuestionID))
        &&  (answer.strAnswer!=" " || answer.strAnswer.Trim()==string.Empty)
        && answer.nQuestionnnaireId==Convert.ToInt32(Session["FormId"].ToString())
    select answer).ToList();

List<int> list = new List<int>();
foreach (var i in ans) list.Add(i.nID);
if (list.Contains(Convert.ToInt32(questionName)))
{
    PopulateSurvey();
}

but when i run the page i got run time error that

Additional information: Unable to cast object of type 'System.Int32[]' to type 'System.IConvertible'.

I want to match id's from my linq query in my int array. If there is a way please let me know.

4
  • You cannot convert a int[] to an int. Perhaps you want to convert every single item . But why they are already Integers? Commented Jun 2, 2015 at 7:13
  • 1
    What is the type of nQuestionId in the database? Commented Jun 2, 2015 at 7:14
  • 3
    questionName is an array, you can't convert an array to an integer. It's not clear what you're trying to check with list.Contains(Convert.ToInt32(questionName)) Commented Jun 2, 2015 at 7:14
  • no typecast array to int if nQuestion(list.Contains(questionName)) Commented Jun 2, 2015 at 7:18

4 Answers 4

1

The problem is in the line

  list.Contains(Convert.ToInt32(questionName))

since questionName is int[] (array) you can't convert it into a single int. You may want either to find out

If list contains all questionName values (and may be some more items)

list.Intersect(questionName).OrderBy(x => x).SequenceEqual(questionName.OrderBy(x => x))

Or if list contains at least one questionName value

list.Intersect(questionName).Any()
Sign up to request clarification or add additional context in comments.

1 Comment

SequenceEqual requires the items in both lists to be in the same order, which may not be the case.
1

I'm not sure but it looks like what you need is to invoke PopulateSurvey if any of the ids retrieved from the database is included in questionName. In that case you can do the following:

if(list.Intersect(questionName).Any())
    PopulateSurvey();

Additional suggestion: do not retrieve the whole records from the database if all you need is the id. So: var list = ... select answer.nId (instead of select answer), note also that by doing this you don't need to manually generate the list anymore.

UPDATE: If what you need is all the items in list to be contained in questionName, use the following (OrderBy is needed because SequenceEqual requires the items to be in the same order in both lists):

list.Intersect(questionName).OrderBy(x=>x).SequenceEqual(questionName.OrderBy(x=>x))

1 Comment

hi i wants to match my all array items of int[] questionName = { 19294, 19300, 20575, 20826, 20827 }; this one if all match than go into if condition otherwise skip the if part
0

You should iterate through the 'questionName' array.

Then for every item in it, check if the list contains it.

Or with Linq you can convert questionName to a list with ToList. After that you can do some magic.

Comments

0

Your problem is in this line

if (list.Contains(Convert.ToInt32(questionName))) //you are trying to convert an array to int

If you want to check that if questionName is a subset of list then change if condition to the following

if (!questionName.Except(list).Any())

Comments

Your Answer

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