Search Query in Linq with EF -
lets have following classes entity framework 5 code first. need search of industries or divisions array of keywords, returning leads match of keywords. need search name of lead same keywords. i'm stuck on how search multiple keywords.
main class
public class lead { public guid id { get; set; } public string name { get; set; } public virtual icollection<industry> industries { get; set; } public virtual icollection<division> divisions { get; set; } }
industry class
public class industry { public guid id { get; set; } public string name { get; set; } public icollection<lead> leads { get; set; } }
division class
public class division { public guid id { get; set; } public string name { get; set; } public icollection<lead> leads { get; set; } }
service / repository call
public iqueryable<lead> getbykeywords(string keyword) { var result = leadrepository.getall().where (x => x.industries.any(i => i.name == keyword) || x.divisions.any(d => d.name == keyword) || x.name.contains(keyword)); return result; }
the above query works single keyword. not work if have multiple words in string , want match of individual keywords.
public ienumerable<lead> getbykeywords(string[] keywords) { var result = getall().where (x =>x.industries.any(i => keywords.any(kw=>kw==i.name)) || x.divisions.any(d =>keywords.any(k=>x.name==k)) || keywords.any(kew => x.name.contains(kew))); return result; }
Comments
Post a Comment