Use of "Any" in specification
ChristPetitjean opened this issue · 2 comments
ChristPetitjean commented
Hello,
I'm facing a fancy problem.
I can't use Any in the specification condition
I think the example is self explanatory:
//... irrevelant stuff to get an instance of repository
Parent parent = new Parent {
// .... irrevelant other fields
array_of_ids_of_children = [1,2,3]
};
Specification<Children> specification = new Specification<Children>();
specification.Conditions.Add(c => parent.array_of_ids_of_children.Any(t => t == c.Id)); // Here, the array is detached from the context since Parent is a new Entity
var result = repository.GetListAsync(specification); // <-- will throw has "Any" will not be resolved correctly despite beeing an Any on an array of primitive type (here an array of integers)
return result;
Have you experinced the same issue ?
I think it come from the dynamic lambda construction.
TanvirArjel commented
@ChristPetitjean Thank you so much for submitting the issue. I am investigating this.
TanvirArjel commented
@ChristPetitjean I have investigated and found that Any()
is working fine in the Specification
. Here is my experiment:
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public Employee[] Employees { get; set; }
}
public class Employee
{
[Key]
public long EmployeeId { get; set; }
public int DepartmentId { get; set; }
[Required]
public string EmployeeName { get; set; }
[Required]
public string DepartmentName { get; set; }
public Department Department { get; set; }
}
Then here is the query:
Specification<Department> specification = new Specification<Department>();
specification.Conditions.Add(d => d.Employees.Any(e => e.EmployeeName == "Tanvir Ahmad"));
List<Department> departments = await _repository.GetListAsync<Department>(specification);