我想知道我該怎麼做? 事實是我有一個對象與一個搜索條件(一些領域來搜索哪個意味着條件)。我需要根據條件構建查詢,但是它與AND一起加入。 這是我有:實體框架構建OR並傳遞到Where
public class SearchCriteria
{
public SearchCriteria()
{
this.Theme = new HashSet<int>();
}
public string KeyWord { get; set; }
public bool? ChildrenFirstCycle { get; set; }
public bool? ChildrenSecondCycle { get; set; }
public bool? Primary { get; set; }
public bool? Secondary { get; set; }
public bool? BachelorArts { get; set; }
public bool? BachelorHumanities { get; set; }
public bool? BachelorScience { get; set; }
public bool? University { get; set; }
public bool? MediumLevelCycle { get; set; }
public bool? HighLevelCycle { get; set; }
public int? DistrictID { get; set; }
public int? CountyID { get; set; }
public int? MunicipalID { get; set; }
public ICollection<int> Theme { get; set; }
}
public IEnumerable<School> GetAllEscuelasBySearchCriteria(SearchCriteria searchCriteria)
{
var Query = this._repository.Retrieve();
if (!string.IsNullOrWhiteSpace(searchCriteria.KeyWord))
{
Query = Query.Where(p => p.Name.Contains(searchCriteria.KeyWord) || p.Grade.Contains(searchCriteria.KeyWord) || p.Email.Contains(searchCriteria.KeyWord) || p.Address.Contains(searchCriteria.KeyWord) || p.Code.Contains(searchCriteria.KeyWord));
}
//Rest of condition
if (RestConditions)
{
Query = Query.Where(p=> REST CONDITIONS);
}
//I need to create a list of func expression and add each expression pass the condition and then a foreach and do an OR for all in List of FUNC
if (searchCriteria.InfantilFirstCycle.HasValue && searchCriteria.InfantilFirstCycle.Value != false)
{
Query = Query.Where(p => (p.value1 > 0 || p.value2 > 0 || p.value3 > 0));
}
if (searchCriteria.InfantilSecondCycle.HasValue && searchCriteria.InfantilSecondCycle.Value != false)
{
Query = Query.Where(p => (p.value4 > 0 || p.value5 > 0 || p.value6 > 0));
}
if (searchCriteria.Primary.HasValue && searchCriteria.Primary.Value != false)
{
Query = Query.Where(p => (p.value7 > 0 || p.value8 > 0 || p.value9 > 0 || p.value10 > 0 || p.value11 > 0 || p.value13 > 0 || p.value14 > 0));
}
return Query.Distinct().OrderBy(c=>c.Name).ToList();
}
我有這個確切的問題,並最終使用動態linq.https://github.com/kahanu/System.Linq.Dynamic – Mansfield
我試過但說基地{System.Exception} = {「在LINQ to Entities不支持LINQ表達式節點類型'Invoke'。」} – user2528557