9

I am trying to pass an array int[] to my function, then delete all records with a primary key in that array.

This line .Where(t => personIds.Contains(t.PersonId)).ToList() throws the error:

'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:

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;}
 }
}
9
  • 3
    check for a using System.Linq; directive. You need it.
    – ℍ ℍ
    Jul 7, 2014 at 20:09
  • 4
    Is t.PersonId really an int?
    – dee-see
    Jul 7, 2014 at 20:10
  • Try making personIds a HashSet. That does have a Contains method. Jul 7, 2014 at 20:10
  • 1
    @HenkHolterman The error message wouldn't reference IQueryable.Contains<TSource> if he didn't have that using.
    – Servy
    Jul 7, 2014 at 20:14
  • Why mark the question down? I don't understand? Jul 7, 2014 at 20:19

1 Answer 1

12

The problem is the the type of t.PersonId was int? I have now added .Value which changed the problem line to .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();
        }
7
  • 1
    What is t.PersonId? Depending on the type, a cast will allow it to compile, but blow up at runtime.
    – TyCobb
    Jul 7, 2014 at 20:16
  • 1
    Apparently it's not an int.
    – ℍ ℍ
    Jul 7, 2014 at 20:23
  • 1
    Weird, you should not have needed the cast.
    – TyCobb
    Jul 7, 2014 at 20:23
  • 2
    That's a nullable int. That makes more sense. You could call .Value off of it instead of casting if you know it will never be null.
    – TyCobb
    Jul 7, 2014 at 20:29
  • 1
    int? != int int? is a nullable integer, thus it can also take the value of null what an ordinary int can't do. There is no implicit cast from int? i to int as this causes an exception if the value is null. You should also check for null values in your WHERE clause, otherwise you will get exceptions if t.PersonID is null. And furthermore, you should reconsider if having a nullable primary key is a good idea (or remove the nullable if you are sure the value will never be null) Jul 7, 2014 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.