c# - LINQ: search 2 properties for a list of strings -
i have following in varies parts of code:
questionmodel:
public class questionmodel { public string question { get; set; } public string answer { get; set; } }
keywords:
list<string> searchkeywords
questions:
list<questionmodel> questions
what achieve list of questions, search , retain questions have keywords.
i'm gone far hit road block:
var questions = getallquestions(); //returns questions list<questionmodel> questions = questions.all(x => searchkeywords.all(k => x.question.contains(k) || x.answer.contains(k)));
this returns bool.
any or directions appreciated.
you using wrong linq method, want where
instead of all
:
questions = questions.where(x => ...);
all
tells if every item in collection satisfies condition (boolean result); where
filters elements satisfy condition (filtered collection result).
depending on questions
(looks property, of type?) may have wrap tolist
or toarray
.
Comments
Post a Comment