LINQ到SQL错误:“INT []”不包含定义“包含”(Linq-to-sql error: &#

2019-09-17 09:55发布

我有一个错误:

错误2“INT []”不包含“包含”的定义和最佳的扩展方法过载“System.Linq.Enumerable.Contains(System.Collections.Generic.IEnumerable,TSource)”具有一些无效参数

这是我的代码:

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

Answer 1:

我强烈怀疑item.Type不是int 。 它是一个枚举? 如果是的话,尽量明确地铸造:

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

此外,作为点符号:

var query = dc.LeaveRequests.Where(item => validType.Contains((int) item.Type)
                                           && item.MgtApproval == null);


Answer 2:

item.Type不是int



Answer 3:

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

`



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