c# - 'int[ ]' does not contain a definition for ' 包含'

标签 c# linq asp.net-web-api

我正在尝试传递一个数组 int[]到我的函数,然后删除该数组中具有主键的所有记录。

这一行.Where(t => personIds.Contains(t.PersonId)).ToList()抛出错误:

'int[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Queryable.Contains<TSource>(System.Linq.IQueryable<TSource>, TSource)' has some invalid arguments

这是我的 Controller :

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Hercules.WebApi.Models;

namespace Hercules.WebApi.Controllers
{
    public class TicketController : ApiController
    {
        private MyWebApiContext db = new MyWebApiContext();

    [Route("Ticket/removeTicketPeople")]
    public void RemoveTicketPeople([FromUri]int ticketId, [FromBody]int[] personIds)
        {
            db.TicketPeople.Where(t => t.TicketId == ticketId)
                .Where(t => personIds.Contains(t.PersonId)).ToList()
                .ForEach(t => db.TicketPeople.Remove(t));
            db.SaveChanges();
        }

  }
}

这是人物模型:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Hercules.WebApi.Models
{
    public class Person
    {
        [Key]
        public int PersonId { get; set; }

        // Properties

        public String Firstname { get; set; }
        public String Surname { get; set; }
    }
}  

这是 ProjectPerson 链接表模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Hercules.WebApi.Models
{
    public class ProjectPerson{
    [Key]
    public int ProjectPersonId { get; set; }

    [ForeignKey("Project")]
    public int? ProjectId {get;set;}
    public virtual Project Project { get; set; }

    [ForeignKey("Person")]
    public int? PersonId {get;set;}
    public virtual Person Person {get;set;}

    public string RelationshipType {get;set;}
 }
}

最佳答案

问题是 t.PersonId 的类型是 int? 我现在添加了 .Value 将问题行更改为 .Where(t => personIds.Contains(t.PersonId.Value)).ToList().这现在有效:

public void RemoveTicketPeople([FromUri]int ticketId, [FromBody]int[] personIds)
        {
                db.TicketPeople.Where(t => t.TicketId == ticketId)
                .Where(t => personIds.Contains( t.PersonId.Value)).ToList()
                .ForEach(t => db.TicketPeople.Remove(t));
                db.SaveChanges();
        }

关于c# - 'int[ ]' does not contain a definition for ' 包含',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24618916/

相关文章:

c# 我是否正确处理多个线程

c# - LINQ Contains 与 Intersect(与其他任何东西相比!)

c# - 如何检测C#表单中是否按下了多个键

c# - 分组相同类型的集合 C#

c# - 坚持 linq 查询

c# - 使用NEST在ElasticSearch的IEnumerable属性上配置分析器的问题

c# - Linq Find 方法作为 IQueryable

JQuery AJAX方法在JSONP请求上调用 "error"函数而不是 "success"函数

asp.net - AntiForgery Token 使用 ASP.NET5 Web API,在 NET46 上没有 System.Web.Helpers

asp.net-mvc - Web API 2 中具有 MVC 5 AttributeRouting 的 QueryString