繁体   English   中英

'int []'不包含'Contains'的定义

[英]'int[]' does not contain a definition for 'Contains'

我试图将数组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

这是我的控制器:

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

暂无
暂无

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

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