27

I am having an error:

Error 2 'int[]' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.Enumerable.Contains(System.Collections.Generic.IEnumerable, TSource)' has some invalid arguments

This is my code:

public partial class mymymy : System.Web.UI.Page
{
    int[] validType = { 2, 3, 4, 5, 6, 8, 13, 14, 16, 22 };

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        using (var dc = new soxMainDataContext())
        {
            var qry = from item in dc.LeaveRequests
                  where **validType**.Contains(item.Type)
                      && item.MgtApproval == null
                  select item;
            e.Result = qry;
        }
    }
}
Martin Harris
  • 28,277
  • 7
  • 90
  • 101
Yves
  • 12,059
  • 15
  • 53
  • 57

4 Answers4

50

I strongly suspect that item.Type isn't an int. Is it an enum? If so, try explicitly casting:

var qry = from item in dc.LeaveRequests
          where validType.Contains((int) item.Type)
                && item.MgtApproval == null
          select item;

Alternatively, as dot notation:

var query = dc.LeaveRequests.Where(item => validType.Contains((int) item.Type)
                                           && item.MgtApproval == null);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • What if item.Type is an int? (Null able int ) – Avneesh Srivastava Jun 03 '16 at 07:08
  • @AvneeshSrivastava: Then it would depend on what type `validType` was, and what you'd want to do if `item.Type` is null. Assuming this is a problem you're currently facing, I suggest you try various things and then ask a new question with all the relevant details if necessary. – Jon Skeet Jun 03 '16 at 07:19
  • Hi Jon yes I am facing that issue I have asked a new question http://stackoverflow.com/q/37608704/2794391 I have also trying many things from morning please help me out. – Avneesh Srivastava Jun 03 '16 at 07:44
  • and in case item.Type is null-able int. use the item.Type.Value – Dileep Aug 09 '19 at 18:49
4

item.Type isn't an int.

mqp
  • 70,359
  • 14
  • 95
  • 123
3
var consulta = from pr in lsprodcts 
               where pr.nProductoID.ToString().Contains(Idproducto.ToString())
               select new
               {
                   pr.nProductoID,
                   ProdName = pr.cNombre,
                   pr.cDescripcion,
               };

`

Sherif Ahmed
  • 1,896
  • 1
  • 19
  • 37
navi_osoro
  • 31
  • 1
0

Don't forget to import Linq with using System.Linq; at the very top of the class.

Codingwiz
  • 192
  • 2
  • 14