简体   繁体   English

错误:string []不包含“包含”的定义-有效且无效

[英]Error: string[] does not contain a definition for 'Contains' - works and doesn't work

Working on an MVC5 app. 在MVC5应用上工作。 I'm a little confused by this one.... I have 2 variables, both defined as string arrays (msUsers and msSites). 我对此有些困惑。。。我有2个变量,都定义为字符串数组(msUsers和msSites)。 One, msUsers, allows me to use "Contains", the other doesn't. 一个是msUsers,允许我使用“包含”,另一个是不使用。 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. 我有点困惑,因为第一次使用Contains可以对SAME类型的变量正常工作。 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;

The error is indicating a type mismatch, basically. 该错误基本上表示类型不匹配。 Your database column for Branch.id is an integer, and your msSites[] is a string array. 您的Branch.id数据库列是一个整数,而您的msSites[]是一个字符串数组。 You are trying to use Contains to compare a string to an int without doing type conversion. 您正在尝试使用Containsstringint进行比较而不进行类型转换。

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. 选项1由于id似乎始终是一个int,因此您可以重构您的方法以接受int[] msSites ,这可能会解决问题。

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. 选项2如果无法将参数类型更改为方法,则可以尝试在Contains调用中将a.Branch.Id转换为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM