C# Linq-to-sql 错误:“int[]”不包含“Contains”的定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1069106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:29:43  来源:igfitidea点击:

Linq-to-sql error: 'int[]' does not contain a definition for 'Contains'

c#linq-to-sql

提问by Yves

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

错误 2 'int[]' 不包含 'Contains' 的定义,并且最好的扩展方法重载 'System.Linq.Enumerable.Contains(System.Collections.Generic.IEnumerable, TSource)' 有一些无效的参数

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;
        }
    }
}

采纳答案by Jon Skeet

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

我强烈怀疑这item.Type不是int. 是枚举吗?如果是这样,请尝试显式转换:

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);

回答by mqp

item.Typeisn't an int.

item.Type不是int.

回答by navi_osoro

var consulta = from pr in lsprodcts 
               where pr.nProductoID.ToString().Contains(Idproducto.ToString())
               select new
               {
                   pr.nProductoID,
                   ProdName = pr.cNombre,
                   pr.cDescripcion,
               };

`

`