[英]'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.