【问题标题】:does not contain a definiton for where for linq不包含 linq 的 where 的定义
【发布时间】:2021-03-30 10:39:00
【问题描述】:

我想使用 linq 查询从用户表中查询。我在“Users.Where”的这一行收到错误; var KisiList = Users.Where(w => w.Userstatus == 1 && w.Isactive == 1).ToList(); 用户不包含位置的定义。我应该在这里添加什么?

public List<Users> GetBagliOlduguKisiList()
{

     var KisiList = Users.Where(w => w.Userstatus == 1 && w.Isactive == 1).ToList();
    List< Users> users= new List< Users>();
    
    foreach (var item in KisiList )
    {
        Users par= new Users();
        par. ID= item. ID;
        par. Name= item. Name;
        users.Add(par);
    }

    return users;
}

【问题讨论】:

  • 你的 DBContext 在哪里?
  • 我应该在哪里使用 DBContext?
  • 添加到文件顶部:using System.Linq;

标签: mysql .net linq contains


【解决方案1】:

您可能错过了using System.Linq;。此外,如果您使用的是 LINQ,请尝试有效地执行此操作并仅检索需要的字段。

public List<Users> GetBagliOlduguKisiList()
{
    var users = Users
       .Where(w => w.Userstatus == 1 && w.Isactive == 1)
       .Select(u => new Users
       { 
          ID = u.ID,
          Name = u.Name
       })
       .ToList();

    return users;
}

【讨论】:

  • 我使用 System.Linq 添加,但我想我错过了 Queryable 或 dbcontext 之类的东西
  • 那么,什么是用户?
  • Users 是我创建的类名。
  • 所以,这是你的问题。 Where 只能应用于 IQueryable 或 IEnumerable。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多