0

I want query on two field with multiple data via EntityFramework on Student entity. I want all students that name and family exist in filters together. I excepted that record 9 and 6 return in result with one query. how do this operation.

Data in db is:

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

Filters is:

var filters = new List<NameFamily>
{
    new NameFamily{Name = "Adam", Family = "Freeman"},
    new NameFamily{Name = "Luis", Family = "Figo"},
};

Classes is following:

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;}
}
Mohammad Akbari
  • 4,486
  • 6
  • 43
  • 74

2 Answers2

1

Is this what you are looking for?

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);
   }
}
Dronacharya
  • 1,191
  • 11
  • 13
  • That does not address the problem that the students are to be pulled from the DB with EF and the filter list does not contain entities. – Sefe Jan 01 '17 at 08:14
-1

You can try,

Add property in namefamilydto NameFamily

Public string NameFamily get {return Name + Family}

And your lonq query will be,

Var result = db.students.where(e=> filters.select(m=>m.NameFamily).contains(e.name+e.family)

Please ignore the syntax, because i answered from mobile.

ITGenius
  • 129
  • 2
  • 14