【问题标题】:'int[]' does not contain a definition for 'Contains'“int[]”不包含“包含”的定义
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 什么是t.PersonId?根据类型,强制转换将允许它编译,但会在运行时崩溃。
  • t.PersonId 是主键 (int)
  • 显然不是int。
  • 这是一个可以为空的 int。这更有意义。如果你知道它永远不会为空,你可以调用 .Value 而不是强制转换。
  • int? != int int?是一个可为空的整数,因此它也可以采用普通 int 无法做到的null 的值。没有来自 int 的隐式转换? i 到 int,因为如果值为 null,则会导致异常。您还应该检查 WHERE 子句中的空值,否则如果 t.PersonID 为空,您将得到异常。此外,您应该重新考虑是否有一个可为空的主键是一个好主意(或者如果您确定该值永远不会为空,则删除可为空的)
猜你喜欢
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多