Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用linq加载除另一个集合之外的集合_C#_Linq_Entity Framework_Linq To Sql_Ef Code First - Fatal编程技术网

C# 使用linq加载除另一个集合之外的集合

C# 使用linq加载除另一个集合之外的集合,c#,linq,entity-framework,linq-to-sql,ef-code-first,C#,Linq,Entity Framework,Linq To Sql,Ef Code First,我有以下搜索方法: public List<Employeees> AutoSuggestEmployeee(string keyword, long employeeeTypeId, int count) { return context.Employeees.Where( x => x.EmployeeeName.Contains(keyword) && x.EmployeeeTypeId == employe

我有以下搜索方法:

public List<Employeees> AutoSuggestEmployeee(string keyword,
    long employeeeTypeId, int count)
{
    return context.Employeees.Where(
        x => x.EmployeeeName.Contains(keyword)
        && x.EmployeeeTypeId == employeeeTypeId)
    .Take(count).ToList();
}

但它给出了一个例外,即如果您愿意检索所有数据,然后执行“例外”,则
Except
可以只处理Int、Guid等数据类型,这相对简单:

return context.Employees
              .Where(x => x.EmployeeName.Contains(keyword)
                          && x.EmployeeTypeId == employeeeTypeId)
              // Limit the data *somewhat*
              .Take(count + BadEmployees.Count)
              // Do the rest of the query in-process
              .AsEnumerable()
              .Except(BadEmployees)
              .Take(count)
              .ToList();
或者:

// I'm making some assumptions about property names here...
var badEmployeeIds = badEmployees.Select(x => x.EmployeeId)
                                 .ToList();

return context.Employees
              .Where(x => x.EmployeeName.Contains(keyword)
                          && x.EmployeeTypeId == employeeeTypeId)
                          && !badEmployeeIds.Contains(x.EmployeeId))
              .Take(count)
              .ToList();

Except
方法进行比较,因此它必须知道如何比较对象。对于简单类型,有标准比较,但是对于复杂类型,您需要提供一个相等比较器来比较对象中的相关数据

例如:

class EmployeeComparer : IEqualityComparer<Employeees> {

  public bool Equals(Employeees x, Employeees y) {
    return x.Id == y.Id;
  }

  public int GetHashCode(Employeees employee) {
    return employee.Id.GetHashCode();
  }

}

我使用了您的类,使用方式如下:context.Employees.Where(x=>x.EmployeeName.Contains(关键字)和&x.EmployeeTypeId==EmployeeTypeId)。除了(Employees,new EmployeeComparer()).Take(count.ToList();但是我得到了这个错误:LINQ to Entities不识别方法'System.LINQ.IQueryable
1[ManUp.Data.Employee],除了[Employee](System.LINQ.IQueryable
1[ManUp.Data.Employee],System.Collections.Generic.IEqualityComparer1[Data.Employee])方法,而且这个方法不能被转换成存储表达式。在此之前,您需要一个
.AsEnumerable()
,才能从数据库中提取结果。如果你想在数据库中做这项工作,你应该用连接来代替。是的,起初我想用连接来做,但我不是linq方面的专家,所以无法编写,你能在我的例子中给出这个连接的例子吗?
class EmployeeComparer : IEqualityComparer<Employeees> {

  public bool Equals(Employeees x, Employeees y) {
    return x.Id == y.Id;
  }

  public int GetHashCode(Employeees employee) {
    return employee.Id.GetHashCode();
  }

}
return
  context.Employeees
  .Where(x => x.EmployeeeName.Contains(keyword) && x.EmployeeeTypeId == employeeeTypeId)
  .Except(BadEmployeees, new EmployeeComparer())
  .Take(count)
  .ToList();