我想通过Student实体上的EntityFramework查询具有多个数据的两个字段。我希望所有名字和家庭的学生都在过滤器中。我除了记录9和6在一个查询中返回结果。怎么做这个操作。
db中的数据是:
Id Name Family
-------------------------------
1 John Cooper
2 Lee Chang
3 Morgan Freeman
4 Luis Enrique
5 Jack Anderson
6 Adam Freeman
7 Bill Gates
8 David Beckham
9 Luis Figo
过滤器是:
var filters = new List<NameFamily>
{
new NameFamily{Name = "Adam", Family = "Freeman"},
new NameFamily{Name = "Luis", Family = "Figo"},
};
课程如下:
public class Student
{
public int Id{get; set;}
public string Name{get; set;}
public string Family{get; set;}
}
public class NameFamilyDto
{
public string Name{get; set;}
public string Family{get; set;}
}
答案 0 :(得分:1)
这是你在找什么?
var studentList = new List<Student>();
foreach (var filter in filters)
{
var student = _dbContext.Set<Student>.where(x => x.Name == filter.Name && x.family == filter.family);
if (student != null)
{
studentList.Add(student);
}
}
答案 1 :(得分:-1)
你可以尝试,
在namefamilydto NameFamily中添加属性
公共字符串NameFamily获取{return Name + Family}
你的lonq查询将是,
Var result = db.students.where(e =&gt; filters.select(m =&gt; m.NameFamily).contains(e.name + e.family)
请忽略语法,因为我是从移动设备上回答的。