c# - Linq-to-sql 错误 : 'int[]' does not contain a definition for 'Contains'

标签 c# linq-to-sql

我有一个错误:

错误 2 'int[]' 不包含 'Contains' 的定义并且最佳扩展方法重载 '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;
        }
    }
}

最佳答案

我强烈怀疑 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);

关于c# - Linq-to-sql 错误 : 'int[]' does not contain a definition for 'Contains' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1069106/

相关文章:

c# - MongoDB C# 查询包含属性值的对象数组

c# - sql中如何获取 "TryCast"数据

c# - 使用 linq to sql 中的方法属性处理事务上下文

linq - 如何合并两个 Linq 查询的两个输出?

asp.net-mvc - ASP MVC : When is IController Dispose() called?

c# - 通过 GUI 访问 Windows 服务的最佳方式

c# - 如何更改所有月份的所有工作日的颜色属性

c# - 对于需要在持久化之前创建关系的实体插入,我的代码应该放在哪里?

asp.net-mvc - LINQ to SQL 在压力测试时抛出异常

.net - 无法让 Left JOIN linq 查询工作!