Dynamic library (System.Linq.Dynamic) SQL LIKE Operator












1















Someone has posted a similar question here How Dynamic library (System.Linq.Dynamic) support LIKE Operator?



But it's not exactly what I want. The Contains operator mentioned in that post only do this in SQL "%SOMETHING%". But the LIKE operator in SQL can do this "SOME%THING". Is there a similar operator for Dynamic LINQ? If not, is there a solution for this? Maybe with Regex? Also is there a single character wildcard? E.g. "SOM$THING" returns "SOMETHING" or "SOM3THING"



Edit: I am developing a WPF application which should read log files in XML format. Each element contains 34 fields. So instead of writing a very long WHERE, I have used System.Reflection.PropertyInfo to iterate each field to write the query and then use System.Linq.Dynamic to execute it.



Edit2: I have edited the code so it's more readable for the viewers.



Here is some code:



Example 1:
prop.Name = "FirstName", paramterString = "Ke%in", returns "Kevin",
"Kelvin"...
Example 2:
prop.Name = "FirstName", paramterString = "Ke$in", returns "Kevin",
"Kelin"...



var query = "";
StringBuilder sb = new StringBuilder();
foreach (var prop in stringProps)
{
sb.Append($"({prop.Name} != null And {prop.Name}.Contains({parameterString})");
}
query = sb.ToString().Substring(0, sb.Length - 4);
filteredData = filteredData.Where(query);


One of the requirement is to implement a SQL LIKE operator, so the users can use something like this to get the result they want: FirstName LIKE 'SOME%THING'










share|improve this question

























  • Can you restrict your project to MS SQL Server? Then you could extend Dynamic LINQ to support SQLMethods.Like.

    – NetMage
    Nov 16 '18 at 18:56











  • Unfortunately this is not possible, because the data are imported by reading XML files.

    – Kevin Man
    Nov 19 '18 at 9:12






  • 1





    On the post you linked, there's an answer suggesting to use a combination of Contains, StartsWits, EndsWith. Can this work for you? You can also use a Regex-Like, as shown here. But a pure LIKE is something that LInq doesn't provide.

    – HeyJude
    Nov 19 '18 at 12:08











  • That's what I am afraid of. So I have used String.IndexOf() to create my own LIKE operator. If someone can review the code or even better, optimize it, I would be appreciate it. The code is posted in the answer.

    – Kevin Man
    Nov 19 '18 at 14:09











  • It begins to sound like this has nothing to do with SQL. If you just want to implement a LIKE style operator for LINQ to Objects, I would suggest translating SQL LIKE patterns to RE, and using the C# Regular Expression handling.

    – NetMage
    Nov 19 '18 at 19:48
















1















Someone has posted a similar question here How Dynamic library (System.Linq.Dynamic) support LIKE Operator?



But it's not exactly what I want. The Contains operator mentioned in that post only do this in SQL "%SOMETHING%". But the LIKE operator in SQL can do this "SOME%THING". Is there a similar operator for Dynamic LINQ? If not, is there a solution for this? Maybe with Regex? Also is there a single character wildcard? E.g. "SOM$THING" returns "SOMETHING" or "SOM3THING"



Edit: I am developing a WPF application which should read log files in XML format. Each element contains 34 fields. So instead of writing a very long WHERE, I have used System.Reflection.PropertyInfo to iterate each field to write the query and then use System.Linq.Dynamic to execute it.



Edit2: I have edited the code so it's more readable for the viewers.



Here is some code:



Example 1:
prop.Name = "FirstName", paramterString = "Ke%in", returns "Kevin",
"Kelvin"...
Example 2:
prop.Name = "FirstName", paramterString = "Ke$in", returns "Kevin",
"Kelin"...



var query = "";
StringBuilder sb = new StringBuilder();
foreach (var prop in stringProps)
{
sb.Append($"({prop.Name} != null And {prop.Name}.Contains({parameterString})");
}
query = sb.ToString().Substring(0, sb.Length - 4);
filteredData = filteredData.Where(query);


One of the requirement is to implement a SQL LIKE operator, so the users can use something like this to get the result they want: FirstName LIKE 'SOME%THING'










share|improve this question

























  • Can you restrict your project to MS SQL Server? Then you could extend Dynamic LINQ to support SQLMethods.Like.

    – NetMage
    Nov 16 '18 at 18:56











  • Unfortunately this is not possible, because the data are imported by reading XML files.

    – Kevin Man
    Nov 19 '18 at 9:12






  • 1





    On the post you linked, there's an answer suggesting to use a combination of Contains, StartsWits, EndsWith. Can this work for you? You can also use a Regex-Like, as shown here. But a pure LIKE is something that LInq doesn't provide.

    – HeyJude
    Nov 19 '18 at 12:08











  • That's what I am afraid of. So I have used String.IndexOf() to create my own LIKE operator. If someone can review the code or even better, optimize it, I would be appreciate it. The code is posted in the answer.

    – Kevin Man
    Nov 19 '18 at 14:09











  • It begins to sound like this has nothing to do with SQL. If you just want to implement a LIKE style operator for LINQ to Objects, I would suggest translating SQL LIKE patterns to RE, and using the C# Regular Expression handling.

    – NetMage
    Nov 19 '18 at 19:48














1












1








1








Someone has posted a similar question here How Dynamic library (System.Linq.Dynamic) support LIKE Operator?



But it's not exactly what I want. The Contains operator mentioned in that post only do this in SQL "%SOMETHING%". But the LIKE operator in SQL can do this "SOME%THING". Is there a similar operator for Dynamic LINQ? If not, is there a solution for this? Maybe with Regex? Also is there a single character wildcard? E.g. "SOM$THING" returns "SOMETHING" or "SOM3THING"



Edit: I am developing a WPF application which should read log files in XML format. Each element contains 34 fields. So instead of writing a very long WHERE, I have used System.Reflection.PropertyInfo to iterate each field to write the query and then use System.Linq.Dynamic to execute it.



Edit2: I have edited the code so it's more readable for the viewers.



Here is some code:



Example 1:
prop.Name = "FirstName", paramterString = "Ke%in", returns "Kevin",
"Kelvin"...
Example 2:
prop.Name = "FirstName", paramterString = "Ke$in", returns "Kevin",
"Kelin"...



var query = "";
StringBuilder sb = new StringBuilder();
foreach (var prop in stringProps)
{
sb.Append($"({prop.Name} != null And {prop.Name}.Contains({parameterString})");
}
query = sb.ToString().Substring(0, sb.Length - 4);
filteredData = filteredData.Where(query);


One of the requirement is to implement a SQL LIKE operator, so the users can use something like this to get the result they want: FirstName LIKE 'SOME%THING'










share|improve this question
















Someone has posted a similar question here How Dynamic library (System.Linq.Dynamic) support LIKE Operator?



But it's not exactly what I want. The Contains operator mentioned in that post only do this in SQL "%SOMETHING%". But the LIKE operator in SQL can do this "SOME%THING". Is there a similar operator for Dynamic LINQ? If not, is there a solution for this? Maybe with Regex? Also is there a single character wildcard? E.g. "SOM$THING" returns "SOMETHING" or "SOM3THING"



Edit: I am developing a WPF application which should read log files in XML format. Each element contains 34 fields. So instead of writing a very long WHERE, I have used System.Reflection.PropertyInfo to iterate each field to write the query and then use System.Linq.Dynamic to execute it.



Edit2: I have edited the code so it's more readable for the viewers.



Here is some code:



Example 1:
prop.Name = "FirstName", paramterString = "Ke%in", returns "Kevin",
"Kelvin"...
Example 2:
prop.Name = "FirstName", paramterString = "Ke$in", returns "Kevin",
"Kelin"...



var query = "";
StringBuilder sb = new StringBuilder();
foreach (var prop in stringProps)
{
sb.Append($"({prop.Name} != null And {prop.Name}.Contains({parameterString})");
}
query = sb.ToString().Substring(0, sb.Length - 4);
filteredData = filteredData.Where(query);


One of the requirement is to implement a SQL LIKE operator, so the users can use something like this to get the result they want: FirstName LIKE 'SOME%THING'







c# sql linq dynamic-linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 11:52







Kevin Man

















asked Nov 16 '18 at 10:47









Kevin ManKevin Man

3812




3812













  • Can you restrict your project to MS SQL Server? Then you could extend Dynamic LINQ to support SQLMethods.Like.

    – NetMage
    Nov 16 '18 at 18:56











  • Unfortunately this is not possible, because the data are imported by reading XML files.

    – Kevin Man
    Nov 19 '18 at 9:12






  • 1





    On the post you linked, there's an answer suggesting to use a combination of Contains, StartsWits, EndsWith. Can this work for you? You can also use a Regex-Like, as shown here. But a pure LIKE is something that LInq doesn't provide.

    – HeyJude
    Nov 19 '18 at 12:08











  • That's what I am afraid of. So I have used String.IndexOf() to create my own LIKE operator. If someone can review the code or even better, optimize it, I would be appreciate it. The code is posted in the answer.

    – Kevin Man
    Nov 19 '18 at 14:09











  • It begins to sound like this has nothing to do with SQL. If you just want to implement a LIKE style operator for LINQ to Objects, I would suggest translating SQL LIKE patterns to RE, and using the C# Regular Expression handling.

    – NetMage
    Nov 19 '18 at 19:48



















  • Can you restrict your project to MS SQL Server? Then you could extend Dynamic LINQ to support SQLMethods.Like.

    – NetMage
    Nov 16 '18 at 18:56











  • Unfortunately this is not possible, because the data are imported by reading XML files.

    – Kevin Man
    Nov 19 '18 at 9:12






  • 1





    On the post you linked, there's an answer suggesting to use a combination of Contains, StartsWits, EndsWith. Can this work for you? You can also use a Regex-Like, as shown here. But a pure LIKE is something that LInq doesn't provide.

    – HeyJude
    Nov 19 '18 at 12:08











  • That's what I am afraid of. So I have used String.IndexOf() to create my own LIKE operator. If someone can review the code or even better, optimize it, I would be appreciate it. The code is posted in the answer.

    – Kevin Man
    Nov 19 '18 at 14:09











  • It begins to sound like this has nothing to do with SQL. If you just want to implement a LIKE style operator for LINQ to Objects, I would suggest translating SQL LIKE patterns to RE, and using the C# Regular Expression handling.

    – NetMage
    Nov 19 '18 at 19:48

















Can you restrict your project to MS SQL Server? Then you could extend Dynamic LINQ to support SQLMethods.Like.

– NetMage
Nov 16 '18 at 18:56





Can you restrict your project to MS SQL Server? Then you could extend Dynamic LINQ to support SQLMethods.Like.

– NetMage
Nov 16 '18 at 18:56













Unfortunately this is not possible, because the data are imported by reading XML files.

– Kevin Man
Nov 19 '18 at 9:12





Unfortunately this is not possible, because the data are imported by reading XML files.

– Kevin Man
Nov 19 '18 at 9:12




1




1





On the post you linked, there's an answer suggesting to use a combination of Contains, StartsWits, EndsWith. Can this work for you? You can also use a Regex-Like, as shown here. But a pure LIKE is something that LInq doesn't provide.

– HeyJude
Nov 19 '18 at 12:08





On the post you linked, there's an answer suggesting to use a combination of Contains, StartsWits, EndsWith. Can this work for you? You can also use a Regex-Like, as shown here. But a pure LIKE is something that LInq doesn't provide.

– HeyJude
Nov 19 '18 at 12:08













That's what I am afraid of. So I have used String.IndexOf() to create my own LIKE operator. If someone can review the code or even better, optimize it, I would be appreciate it. The code is posted in the answer.

– Kevin Man
Nov 19 '18 at 14:09





That's what I am afraid of. So I have used String.IndexOf() to create my own LIKE operator. If someone can review the code or even better, optimize it, I would be appreciate it. The code is posted in the answer.

– Kevin Man
Nov 19 '18 at 14:09













It begins to sound like this has nothing to do with SQL. If you just want to implement a LIKE style operator for LINQ to Objects, I would suggest translating SQL LIKE patterns to RE, and using the C# Regular Expression handling.

– NetMage
Nov 19 '18 at 19:48





It begins to sound like this has nothing to do with SQL. If you just want to implement a LIKE style operator for LINQ to Objects, I would suggest translating SQL LIKE patterns to RE, and using the C# Regular Expression handling.

– NetMage
Nov 19 '18 at 19:48












2 Answers
2






active

oldest

votes


















0














Since there is no LIKE operator in Dynamic Linq library, I have created it my own version. The code is not pretty, but it does work. Hopefully someone can optimize it or suggest a better way to do this.



public string ParseWildcardInParameterString(string parameter, string propertyName)
{
string stringWithWildcard = parameter;
if (parameter.Contains("%") || parameter.Contains("$"))
{
stringWithWildcard = parameter;
string substrings = parameter.Split(new Char { '%', '$' }, StringSplitOptions.RemoveEmptyEntries);
string wildcards = ParseWildcards(parameter);
if (substrings.Any())
{
StringBuilder sb = new StringBuilder();
int substringsCount = substrings.Length;
for (int i = 0; i < substringsCount; i++)
{
if (!substrings[i].EndsWith("\"))
{
int index = parameter.IndexOf(substrings[i]);
if (i < substringsCount - 1)
{
index = parameter.IndexOf(substrings[i + 1], index + 1);
if (index > -1)
{
string secondPart = wildcards[i].Equals("%") ?
$"{propertyName}.IndexOf("{substrings[i + 1]}", {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length) > -1" :
$"{propertyName}.IndexOf("{substrings[i + 1]}", {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length + 1) == {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length + 1";
sb.Append($"({propertyName}.IndexOf("{substrings[i]}") > -1 And {secondPart}) And ");
}
}
}
}
stringWithWildcard = sb.Remove(sb.Length - 5, 5).Append(") Or ").ToString();
}
}
return stringWithWildcard;
}

private string ParseWildcards(string parameter)
{
IList<string> wildcards = new List<string>();
foreach (var chararcter in parameter.ToCharArray())
{
if (chararcter.Equals('%') || chararcter.Equals('$'))
{
wildcards.Add(chararcter.ToString());
}
}
return wildcards.ToArray();
}





share|improve this answer

































    0














    Can you try if the Like functionality in System.Linq.Dynamic.Core does work for you?



    Code example would be:



    var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, "%a%")");



    For full example, see ConsoleAppEF2.1.1/Program.cs






    share|improve this answer
























    • I got an error "DynamicFunctions is not valid" or something.

      – Kevin Man
      Nov 22 '18 at 16:07











    • Are you using EF 2.x ?

      – Stef Heyenrath
      Nov 22 '18 at 17:05











    • No, I just use standard Dynamic Linq. This code actually execute the filter. filteredData = filteredData.Where(query, parameters.ToArray());

      – Kevin Man
      Nov 23 '18 at 8:45











    • For that to work, you need the NuGet nuget.org/packages/Microsoft.EntityFrameworkCore.DynamicLinq

      – Stef Heyenrath
      Nov 23 '18 at 20:03












    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336298%2fdynamic-library-system-linq-dynamic-sql-like-operator%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Since there is no LIKE operator in Dynamic Linq library, I have created it my own version. The code is not pretty, but it does work. Hopefully someone can optimize it or suggest a better way to do this.



    public string ParseWildcardInParameterString(string parameter, string propertyName)
    {
    string stringWithWildcard = parameter;
    if (parameter.Contains("%") || parameter.Contains("$"))
    {
    stringWithWildcard = parameter;
    string substrings = parameter.Split(new Char { '%', '$' }, StringSplitOptions.RemoveEmptyEntries);
    string wildcards = ParseWildcards(parameter);
    if (substrings.Any())
    {
    StringBuilder sb = new StringBuilder();
    int substringsCount = substrings.Length;
    for (int i = 0; i < substringsCount; i++)
    {
    if (!substrings[i].EndsWith("\"))
    {
    int index = parameter.IndexOf(substrings[i]);
    if (i < substringsCount - 1)
    {
    index = parameter.IndexOf(substrings[i + 1], index + 1);
    if (index > -1)
    {
    string secondPart = wildcards[i].Equals("%") ?
    $"{propertyName}.IndexOf("{substrings[i + 1]}", {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length) > -1" :
    $"{propertyName}.IndexOf("{substrings[i + 1]}", {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length + 1) == {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length + 1";
    sb.Append($"({propertyName}.IndexOf("{substrings[i]}") > -1 And {secondPart}) And ");
    }
    }
    }
    }
    stringWithWildcard = sb.Remove(sb.Length - 5, 5).Append(") Or ").ToString();
    }
    }
    return stringWithWildcard;
    }

    private string ParseWildcards(string parameter)
    {
    IList<string> wildcards = new List<string>();
    foreach (var chararcter in parameter.ToCharArray())
    {
    if (chararcter.Equals('%') || chararcter.Equals('$'))
    {
    wildcards.Add(chararcter.ToString());
    }
    }
    return wildcards.ToArray();
    }





    share|improve this answer






























      0














      Since there is no LIKE operator in Dynamic Linq library, I have created it my own version. The code is not pretty, but it does work. Hopefully someone can optimize it or suggest a better way to do this.



      public string ParseWildcardInParameterString(string parameter, string propertyName)
      {
      string stringWithWildcard = parameter;
      if (parameter.Contains("%") || parameter.Contains("$"))
      {
      stringWithWildcard = parameter;
      string substrings = parameter.Split(new Char { '%', '$' }, StringSplitOptions.RemoveEmptyEntries);
      string wildcards = ParseWildcards(parameter);
      if (substrings.Any())
      {
      StringBuilder sb = new StringBuilder();
      int substringsCount = substrings.Length;
      for (int i = 0; i < substringsCount; i++)
      {
      if (!substrings[i].EndsWith("\"))
      {
      int index = parameter.IndexOf(substrings[i]);
      if (i < substringsCount - 1)
      {
      index = parameter.IndexOf(substrings[i + 1], index + 1);
      if (index > -1)
      {
      string secondPart = wildcards[i].Equals("%") ?
      $"{propertyName}.IndexOf("{substrings[i + 1]}", {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length) > -1" :
      $"{propertyName}.IndexOf("{substrings[i + 1]}", {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length + 1) == {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length + 1";
      sb.Append($"({propertyName}.IndexOf("{substrings[i]}") > -1 And {secondPart}) And ");
      }
      }
      }
      }
      stringWithWildcard = sb.Remove(sb.Length - 5, 5).Append(") Or ").ToString();
      }
      }
      return stringWithWildcard;
      }

      private string ParseWildcards(string parameter)
      {
      IList<string> wildcards = new List<string>();
      foreach (var chararcter in parameter.ToCharArray())
      {
      if (chararcter.Equals('%') || chararcter.Equals('$'))
      {
      wildcards.Add(chararcter.ToString());
      }
      }
      return wildcards.ToArray();
      }





      share|improve this answer




























        0












        0








        0







        Since there is no LIKE operator in Dynamic Linq library, I have created it my own version. The code is not pretty, but it does work. Hopefully someone can optimize it or suggest a better way to do this.



        public string ParseWildcardInParameterString(string parameter, string propertyName)
        {
        string stringWithWildcard = parameter;
        if (parameter.Contains("%") || parameter.Contains("$"))
        {
        stringWithWildcard = parameter;
        string substrings = parameter.Split(new Char { '%', '$' }, StringSplitOptions.RemoveEmptyEntries);
        string wildcards = ParseWildcards(parameter);
        if (substrings.Any())
        {
        StringBuilder sb = new StringBuilder();
        int substringsCount = substrings.Length;
        for (int i = 0; i < substringsCount; i++)
        {
        if (!substrings[i].EndsWith("\"))
        {
        int index = parameter.IndexOf(substrings[i]);
        if (i < substringsCount - 1)
        {
        index = parameter.IndexOf(substrings[i + 1], index + 1);
        if (index > -1)
        {
        string secondPart = wildcards[i].Equals("%") ?
        $"{propertyName}.IndexOf("{substrings[i + 1]}", {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length) > -1" :
        $"{propertyName}.IndexOf("{substrings[i + 1]}", {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length + 1) == {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length + 1";
        sb.Append($"({propertyName}.IndexOf("{substrings[i]}") > -1 And {secondPart}) And ");
        }
        }
        }
        }
        stringWithWildcard = sb.Remove(sb.Length - 5, 5).Append(") Or ").ToString();
        }
        }
        return stringWithWildcard;
        }

        private string ParseWildcards(string parameter)
        {
        IList<string> wildcards = new List<string>();
        foreach (var chararcter in parameter.ToCharArray())
        {
        if (chararcter.Equals('%') || chararcter.Equals('$'))
        {
        wildcards.Add(chararcter.ToString());
        }
        }
        return wildcards.ToArray();
        }





        share|improve this answer















        Since there is no LIKE operator in Dynamic Linq library, I have created it my own version. The code is not pretty, but it does work. Hopefully someone can optimize it or suggest a better way to do this.



        public string ParseWildcardInParameterString(string parameter, string propertyName)
        {
        string stringWithWildcard = parameter;
        if (parameter.Contains("%") || parameter.Contains("$"))
        {
        stringWithWildcard = parameter;
        string substrings = parameter.Split(new Char { '%', '$' }, StringSplitOptions.RemoveEmptyEntries);
        string wildcards = ParseWildcards(parameter);
        if (substrings.Any())
        {
        StringBuilder sb = new StringBuilder();
        int substringsCount = substrings.Length;
        for (int i = 0; i < substringsCount; i++)
        {
        if (!substrings[i].EndsWith("\"))
        {
        int index = parameter.IndexOf(substrings[i]);
        if (i < substringsCount - 1)
        {
        index = parameter.IndexOf(substrings[i + 1], index + 1);
        if (index > -1)
        {
        string secondPart = wildcards[i].Equals("%") ?
        $"{propertyName}.IndexOf("{substrings[i + 1]}", {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length) > -1" :
        $"{propertyName}.IndexOf("{substrings[i + 1]}", {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length + 1) == {propertyName}.IndexOf("{substrings[i]}") + "{substrings[i]}".Length + 1";
        sb.Append($"({propertyName}.IndexOf("{substrings[i]}") > -1 And {secondPart}) And ");
        }
        }
        }
        }
        stringWithWildcard = sb.Remove(sb.Length - 5, 5).Append(") Or ").ToString();
        }
        }
        return stringWithWildcard;
        }

        private string ParseWildcards(string parameter)
        {
        IList<string> wildcards = new List<string>();
        foreach (var chararcter in parameter.ToCharArray())
        {
        if (chararcter.Equals('%') || chararcter.Equals('$'))
        {
        wildcards.Add(chararcter.ToString());
        }
        }
        return wildcards.ToArray();
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 14:50

























        answered Nov 19 '18 at 14:11









        Kevin ManKevin Man

        3812




        3812

























            0














            Can you try if the Like functionality in System.Linq.Dynamic.Core does work for you?



            Code example would be:



            var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, "%a%")");



            For full example, see ConsoleAppEF2.1.1/Program.cs






            share|improve this answer
























            • I got an error "DynamicFunctions is not valid" or something.

              – Kevin Man
              Nov 22 '18 at 16:07











            • Are you using EF 2.x ?

              – Stef Heyenrath
              Nov 22 '18 at 17:05











            • No, I just use standard Dynamic Linq. This code actually execute the filter. filteredData = filteredData.Where(query, parameters.ToArray());

              – Kevin Man
              Nov 23 '18 at 8:45











            • For that to work, you need the NuGet nuget.org/packages/Microsoft.EntityFrameworkCore.DynamicLinq

              – Stef Heyenrath
              Nov 23 '18 at 20:03
















            0














            Can you try if the Like functionality in System.Linq.Dynamic.Core does work for you?



            Code example would be:



            var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, "%a%")");



            For full example, see ConsoleAppEF2.1.1/Program.cs






            share|improve this answer
























            • I got an error "DynamicFunctions is not valid" or something.

              – Kevin Man
              Nov 22 '18 at 16:07











            • Are you using EF 2.x ?

              – Stef Heyenrath
              Nov 22 '18 at 17:05











            • No, I just use standard Dynamic Linq. This code actually execute the filter. filteredData = filteredData.Where(query, parameters.ToArray());

              – Kevin Man
              Nov 23 '18 at 8:45











            • For that to work, you need the NuGet nuget.org/packages/Microsoft.EntityFrameworkCore.DynamicLinq

              – Stef Heyenrath
              Nov 23 '18 at 20:03














            0












            0








            0







            Can you try if the Like functionality in System.Linq.Dynamic.Core does work for you?



            Code example would be:



            var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, "%a%")");



            For full example, see ConsoleAppEF2.1.1/Program.cs






            share|improve this answer













            Can you try if the Like functionality in System.Linq.Dynamic.Core does work for you?



            Code example would be:



            var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, "%a%")");



            For full example, see ConsoleAppEF2.1.1/Program.cs







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 12:05









            Stef HeyenrathStef Heyenrath

            4,31694583




            4,31694583













            • I got an error "DynamicFunctions is not valid" or something.

              – Kevin Man
              Nov 22 '18 at 16:07











            • Are you using EF 2.x ?

              – Stef Heyenrath
              Nov 22 '18 at 17:05











            • No, I just use standard Dynamic Linq. This code actually execute the filter. filteredData = filteredData.Where(query, parameters.ToArray());

              – Kevin Man
              Nov 23 '18 at 8:45











            • For that to work, you need the NuGet nuget.org/packages/Microsoft.EntityFrameworkCore.DynamicLinq

              – Stef Heyenrath
              Nov 23 '18 at 20:03



















            • I got an error "DynamicFunctions is not valid" or something.

              – Kevin Man
              Nov 22 '18 at 16:07











            • Are you using EF 2.x ?

              – Stef Heyenrath
              Nov 22 '18 at 17:05











            • No, I just use standard Dynamic Linq. This code actually execute the filter. filteredData = filteredData.Where(query, parameters.ToArray());

              – Kevin Man
              Nov 23 '18 at 8:45











            • For that to work, you need the NuGet nuget.org/packages/Microsoft.EntityFrameworkCore.DynamicLinq

              – Stef Heyenrath
              Nov 23 '18 at 20:03

















            I got an error "DynamicFunctions is not valid" or something.

            – Kevin Man
            Nov 22 '18 at 16:07





            I got an error "DynamicFunctions is not valid" or something.

            – Kevin Man
            Nov 22 '18 at 16:07













            Are you using EF 2.x ?

            – Stef Heyenrath
            Nov 22 '18 at 17:05





            Are you using EF 2.x ?

            – Stef Heyenrath
            Nov 22 '18 at 17:05













            No, I just use standard Dynamic Linq. This code actually execute the filter. filteredData = filteredData.Where(query, parameters.ToArray());

            – Kevin Man
            Nov 23 '18 at 8:45





            No, I just use standard Dynamic Linq. This code actually execute the filter. filteredData = filteredData.Where(query, parameters.ToArray());

            – Kevin Man
            Nov 23 '18 at 8:45













            For that to work, you need the NuGet nuget.org/packages/Microsoft.EntityFrameworkCore.DynamicLinq

            – Stef Heyenrath
            Nov 23 '18 at 20:03





            For that to work, you need the NuGet nuget.org/packages/Microsoft.EntityFrameworkCore.DynamicLinq

            – Stef Heyenrath
            Nov 23 '18 at 20:03


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336298%2fdynamic-library-system-linq-dynamic-sql-like-operator%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine