Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6865021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:59:17+00:00 2026-05-27T02:59:17+00:00

Consider this code: static void Main(string[] args) { var persons = new List<Person> {

  • 0

Consider this code:

    static void Main(string[] args)
    {
        var persons = new List<Person>
                          {
                              new Person("Name1", "Surname1", 30),
                              new Person("Name2", "Surname2", 20),
                              new Person("Name3", "Surname3", 10)
                          };

        var filters = new List<Func<Person, bool>>
                          {
                              p => p.Age > 10,
                              p => p.Surname.Contains("2")
                          };

        var personsFiltered = new List<Person>(persons);
        foreach (var filter in filters)
        {
            personsFiltered = personsFiltered.Where(filter).ToList();
        }

        foreach (var person in personsFiltered)
        {
            Console.WriteLine(person);
        }

        Console.ReadKey();
    }
}

class Person
{
    public string Name { get; private set; }
    public string Surname { get; private set; }
    public int Age { get; private set; }

    public Person(string name, string surname, int age)
    {
        Name = name;
        Surname = surname;
        Age = age;
    }

    public override string ToString()
    {
        return String.Format("Name:{0}, Surname:{1}, Age:{2}", Name, Surname, Age);
    }
}

Is it possible to pass filters as string parameters, so that the user could create custom filters? For example, the filters list in my example would initially be empty and the user would provide strings “p => p.Age > 10” and “p => p.Surname.Contains(\”2″)”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:59:17+00:00Added an answer on May 27, 2026 at 2:59 am

    You have two options to allow the user to write code for your application : A scripting engine and dynamic compilation.

    LUA

    You could integrate a scripting engine like Lua (I found that very easy) : /technical/game-programming/using-lua-with-c-r2275″>http://www.gamedev.net/page/resources//technical/game-programming/using-lua-with-c-r2275

    You need to create a Lua environment, set the variables to be used by the user’s script, then read the variables from that environment. I built a partly scripted printing template engine using this method and it worked perfectly. LuaInterface for C# is very intuitive and simple.

    DYNAMIC COMPILATION

    Another option is to implement dynamic compilation (a little more trouble, but more powerful) : http://www.codeproject.com/KB/dotnet/DynamicCompileAndRun.aspx

    You need to create a framework class and concatenate a string to include the user’s script inside a class that implements a certain interface. Something like :

    class ClientScript : IScriptFramework {
        public override String Method(Object object) {
            return %CLIENT_CODE%;
        }
    }
    

    Assuming IScriptFramework declares Method, you can store an isntance of this class into a IScriptFramework and call Method without any reflection. Just store this code in a resource, generate the final source code by replacing %CLIENT_CODE% with the actual code (like object.ToString()) and compiling that in memory as described in the post I linked.

    I used dynamic compilation for a testing framework and it works pretty well, although the syntax is more strict than a script engine like Lua.

    EDIT : Viacheslav Smityukh is right, there is a framework that does the hard job of dynamically compiling LINQ queries for you. I read about it a while back, but I didn’t think about before I saw his comment. It may be the easiest way to go for you : http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code snippet: class Program { static void Main(string[] args) { Console.WriteLine(Test().ToString()); }
Consider the following code: class Program { static void Main(string[] args) { new Program().Run(args);
Consider this code: class arraytest { public static void main(String args[]) { int[] a
Consider this code: class Jm44 { public static void main(String args[]){ int []arr =
Consider this code: class test { public static void main(String[] args) { test inst_test
Consider the following code: static void Main(string[] args) { using (MemoryStream memoryStream = new
Consider this code snippet: public static void main(String[] args) { int z1 = 0;
Consider this code: public class ShortDivision { public static void main(String[] args) { short
Consider the following code: class Program { static void Main(string[] args) { try {
Consider the following code: namespace ConsoleApplication1 { class Program { static void Main(string[] args)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.