Search code examples
c#stringlambdacontain

lambda search in combined string


Here is my table data ,

 Areas                           Cities
==============================================================
Area1                   City1 , City2 , City3 , City4
Area2                   City5 , City6 , City7 , City8
Area3                   City9 , City10 , City11 , City12
Area4                   City13 , City14 , City15 , City16

If I give the value City1 , I want to get Area1 .
(eg. City7 => Area2 , City14 => Area4) .
Is there any short way to do it using Lambra Expression ?


Solution

  • Try this code:

    yourDataTable.First(x => x.Cities.Split(',').Any(y => y.Trim() == "City1"))
    

    Where search term is string like City 1