Reputation: 1249
Working on an MVC5 app. I'm a little confused by this one.... I have 2 variables, both defined as string arrays (msUsers and msSites). One, msUsers, allows me to use "Contains", the other doesn't. Here's a snippet of the code....
private IQueryable<Event> GetEventsFiltered(
string userName,
string workStatus,
string[] msUsers,
string[] msSites,
int? eventTypeId)
{
IQueryable<Event> events = this.db.Events;
if (msUsers != null)
{
events = events.Include(a => a.AspNetUser)
.Where(a => msUsers.Contains(a.AspNetUser.Id));
}
if (msSites != null)
{
// It doesn't like Contains here....
events = events.Include(a => a.Branch)
.Where(a => msSites.Contains(a.Branch.Id));
}
The error (for the last "Contains")...
'string[]' does not contain a definition for 'Contains' and the best extension method
overload 'Queryable.Contains<int>(IQueryable<int>, int)' requires a receiver of type
'IQueryable<int>'
I'm a little confused because the first usage of Contains works fine against the SAME type of variable. Any suggestions/ideas? Thanks!
***** UPDATED to add NAMESPACES ******
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using System.Web.Security;
using AuditingApp.Models;
using AuditingApp.Services;
using AuditingApp.ViewModels;
using Microsoft.AspNet.Identity;
Upvotes: 1
Views: 5876
Reputation: 3540
The error is indicating a type mismatch, basically. Your database column for Branch.id
is an integer, and your msSites[]
is a string array. You are trying to use Contains
to compare a string
to an int
without doing type conversion.
Option 1
Since the id appears to always be an int, you can refactor your method to accept int[] msSites
and that would likely resolve the problem.
Option 2
If you can't change the argument types to your method, you could try converting a.Branch.Id
to a string in the Contains
call.
Upvotes: 6