【发布时间】:2014-07-07 20:07:04
【问题描述】:
我正在尝试将数组 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;}
}
}
【问题讨论】:
-
检查
using System.Linq;指令。你需要它。 -
t.PersonId真的是int吗? -
尝试将 personIds 设为
HashSet。那确实有一个Contains方法。 -
@HenkHolterman 如果没有
IQueryable.Contains<TSource>,则错误消息不会引用using。 -
为什么要把问题记下来?我不明白?
标签: c# linq asp.net-web-api