2017-01-01 37 views
0

我想通過EntityFramework對Student實體在兩個字段中使用多個數據進行查詢。我希望所有名字和家庭的學生一起存在於過濾器中。我排除了記錄9和6返回一個查詢的結果。這個操作怎麼辦。Linq在兩個字段中包含查詢

數據在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;} 
} 

回答

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); 
    } 
} 
+0

這並沒有解決使用EF從數據庫中拉出學生並且過濾器列表不包含實體的問題。 – Sefe

-1

你可以試試,

在namefamilydto添加屬性NameFamily

公共字符串NameFamily {返回名稱+家庭}

而且你lonq查詢將是

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

請忽略語法,因爲我從移動應答。