Search code examples
asp.net-mvclambdalinq-to-entities

How can search a string array contains metod without loop


I have an array of view model. Now I want to check that view model array to contain a word within an array.

 public IQueryable<CategorisedPostViewModel> GetRelatedPostbyCategories(string categories)
 {
       var ctries = categories.Split(',');
       var result = GetAllCategoriedPost().**Where(p=>p.CategoryName.Contains(ctries)).**OrderByDescending(c => c.Published);
       return result;
  }

How can I search that bold portions without loop?

We may assume for simplicity,

p.categoryName="jerry,tom,ema"

and

ctries={"Gates","jerry","Jobs","ema"}

I want to check if any ctries is found on p.categoryName. Please help me. Thanks in advance.


Solution

  • To check if any category name is present in ctries try, Intersect

    p.categoryName.Intersect(tries).Any()