[英]'int[]' does not contain a definition for 'Contains'
I am trying to pass an array int[]
to my function, then delete all records with a primary key in that array. 我试图将数组int[]
传递给函数,然后删除该数组中具有主键的所有记录。
This line .Where(t => personIds.Contains(t.PersonId)).ToList()
throws the error: 此行.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
This is my 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();
}
}
}
This is the Person Model: 这是人员模型:
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; }
}
}
This is the ProjectPerson link table model: 这是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;}
}
}
The problem is the the type of t.PersonId
was int?
问题是t.PersonId
的类型是int?
I have now added .Value
which changed the problem line to .Where(t => personIds.Contains(t.PersonId.Value)).ToList()
. 我现在添加了.Value
,将问题行更改为.Where(t => personIds.Contains(t.PersonId.Value)).ToList()
。 This now works: 现在可以使用:
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.